-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cqf-Tooling : NewRefreshIg update #528
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
58cbd0b
Added missing tests
4d48c41
_Added CqlRefresh in order to update cql file before conversion.
bd038b7
Formatting
2513f4e
Test update
0cffc31
Test path update
f234eaa
Update test files
2fb61d5
Update test files 2
57c072e
Clean up of test file
fb1b72a
Clean up of test file 2
b0e8efc
This one is not a file path
JPercival b5e059c
Fix output directory
JPercival b69379e
Move files to same directory
JPercival 4bba09a
TEST: don't swallow exceptions
JPercival 4d4ee8a
Add logging of missing files
JPercival 8d95c1f
Revert previous experiments
JPercival 5c7efb0
Fix casing of folder name
JPercival f52d79f
Add path to CQL source information
JPercival 250c3a8
Add more logging
JPercival ea7c406
Fix prefix, fix some level 1.3 warnings
JPercival fc3c1d8
Change build to use info log level for debug purposes
JPercival 4c9e916
Swap up dependency loading code
JPercival 1685c3e
Revert to normal logging
JPercival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
tooling/src/main/java/org/opencds/cqf/tooling/operation/ig/CqlRefresh.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.opencds.cqf.tooling.operation.ig; | ||
|
||
import org.hl7.fhir.instance.model.api.IBaseResource; | ||
import org.opencds.cqf.tooling.parameter.RefreshIGParameters; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class CqlRefresh extends Refresh { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(CqlRefresh.class); | ||
private final Pattern VERSION_PATTERN = Pattern.compile("^(library\\s+(\\S+)\\s+version\\s+)'[0-9]+\\.[0-9]+\\.[0-9]+'"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure whether this would be easier, but it seems like it would definitely be more robust to use the Antlr visitor framework for this? |
||
private final Pattern INCLUDE_PATTERN = Pattern.compile("^(include\\s+(\\S+)\\s+version\\s+)'([0-9]+\\.[0-9]+\\.[0-9]+)'(\\s+called\\s+(\\S+))?"); | ||
|
||
public CqlRefresh(IGInfo igInfo) { | ||
super(igInfo); | ||
} | ||
|
||
@Override | ||
public List<IBaseResource> refresh() { | ||
return List.of(); | ||
} | ||
|
||
public void refreshCql(IGInfo igInfo, RefreshIGParameters params) { | ||
Map<String, String> updatedLibraries = refreshCqlFile(igInfo.getCqlBinaryPath(), params.updatedVersion); | ||
updateCqlReferences(igInfo.getCqlBinaryPath(), updatedLibraries); | ||
} | ||
|
||
private Map<String, String> refreshCqlFile(String cqlBinaryPath, String updatedVersion) { | ||
Map<String, String> updatedLibraries = new HashMap<>(); | ||
try (Stream<Path> paths = Files.walk(Paths.get(cqlBinaryPath))) { | ||
List<Path> files = paths | ||
.filter(Files::isRegularFile) | ||
.filter(path -> path.toString().endsWith(".cql")) | ||
.collect(Collectors.toList()); | ||
|
||
for (Path file : files) { | ||
List<String> lines = Files.readAllLines(file); | ||
for (int i = 0; i < lines.size(); i++) { | ||
Matcher matcher = VERSION_PATTERN.matcher(lines.get(i)); | ||
if (matcher.matches()) { | ||
String libraryName = matcher.group(2); | ||
String updatedLine = matcher.replaceFirst("$1'" + updatedVersion + "'"); | ||
lines.set(i, updatedLine); | ||
Files.write(file, lines); | ||
updatedLibraries.put(libraryName, updatedVersion); | ||
break; | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
logger.error("Error updating cql files: {}", e.getMessage()); | ||
} | ||
return updatedLibraries; | ||
} | ||
|
||
private void updateCqlReferences(String cqlBinaryPath, Map<String, String> updatedLibraries) { | ||
try (Stream<Path> paths = Files.walk(Paths.get(cqlBinaryPath))) { | ||
List<Path> files = paths | ||
.filter(Files::isRegularFile) | ||
.filter(path -> path.toString().endsWith(".cql")) | ||
.collect(Collectors.toList()); | ||
|
||
for (Path file : files) { | ||
List<String> lines = Files.readAllLines(file); | ||
boolean fileUpdated = false; | ||
for (int i = 0; i < lines.size(); i++) { | ||
Matcher matcher = INCLUDE_PATTERN.matcher(lines.get(i)); | ||
while (matcher.find()) { | ||
String libraryName = matcher.group(2); | ||
String newVersion = updatedLibraries.get(libraryName); | ||
if (newVersion != null && !matcher.group(3).equals(newVersion)) { | ||
String calledPart = matcher.group(4) != null ? matcher.group(4) : ""; | ||
String newLine = matcher.replaceFirst("$1'" + newVersion + "'" + calledPart); | ||
lines.set(i, newLine); | ||
fileUpdated = true; | ||
} | ||
} | ||
} | ||
if (fileUpdated) { | ||
Files.write(file, lines); | ||
} | ||
} | ||
} catch (IOException e) { | ||
logger.error("Error updating CQL references: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should call this CqlVersionRefresh, yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll rename it thanks !