Skip to content
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

groovyw module update-all improvements #4009

Merged
merged 18 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion config/groovy/common.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,33 @@ class common {
addRemote(itemName, defaultRemote, "https://github.com/${githubDefaultHome}/${itemName}.git")
}

/**
* Check if an item was updated within the provided time limit
* @param file the item's FETCH_HEAD file in the .git directory
* @param timeLimit the time limit for considering something recently updated, for example: use(groovy.time.TimeCategory){ 10.minute }
*/
def isRecentlyUpdated(File file, def timeLimit){
Date lastUpdate = new Date(file.lastModified())
def recentlyUpdated = use(groovy.time.TimeCategory){
def timeElapsedSinceUpdate = new Date() - lastUpdate
if (timeElapsedSinceUpdate < timeLimit){
return true
} else {
return false
}
}
}

/**
* Update a given item.
* @param itemName the name of the item to update
*/
def updateItem(String itemName) {
def updateItem(String itemName, boolean skipRecentUpdates = false) {
cvennel marked this conversation as resolved.
Show resolved Hide resolved
File targetDir = new File(targetDirectory, itemName)
if (!Character.isLetterOrDigit(itemName.charAt(0))){
println color ("Skipping update for $itemName: starts with non-alphanumeric symbol", Ansi.YELLOW)
return
}
if (!targetDir.exists()) {
println color("$itemType \"$itemName\" not found", Ansi.RED)
return
Expand Down Expand Up @@ -215,8 +236,42 @@ class common {
println color("uncommitted changes. Skipping.", Ansi.YELLOW)
} else {
println color("updating $itemType $itemName", Ansi.GREEN)

File targetDirFetchHead = new File("$targetDir/.git/FETCH_HEAD")
if (targetDirFetchHead.exists()){
// If the FETCH_HEAD has been modified within time limit and -skip-recently-updated flag was passed, skip updating
def timeLimit = use(groovy.time.TimeCategory){ 10.minute }
if (skipRecentUpdates && isRecentlyUpdated(targetDirFetchHead, timeLimit)){
println color("Skipping update for $itemName: updated within last $timeLimit", Ansi.YELLOW)
return
}
// Always update modified time for FETCH_HEAD if it exists
targetDirFetchHead.setLastModified(new Date().getTime())
}

try {
def current_sha = itemGit.log(maxCommits: 1).find().getAbbreviatedId(8)
itemGit.pull remote: defaultRemote
def post_update_sha = itemGit.log(maxCommits: 1).find().getAbbreviatedId(8)

if (current_sha != post_update_sha){
// TODO this can be probably converted to do one composite diff of the full update
// once this PR is merged for grgit: https://github.com/ajoberstar/grgit/pull/318
println color("Updating $current_sha..$post_update_sha", Ansi.GREEN)
cvennel marked this conversation as resolved.
Show resolved Hide resolved
def commits = itemGit.log {range(current_sha, post_update_sha)}
for (commit in commits){
println("----${commit.getAbbreviatedId(8)}----")
def diff = itemGit.show(commit: commit.id)
print("added: ${diff.added.size()}, ")
print("copied: ${diff.copied.size()}, ")
print("modified: ${diff.modified.size()}, ")
print("removed: ${diff.removed.size()}, ")
println("renamed: ${diff.renamed.size()}")
}
print("\n")
} else {
println color ("No changes found", Ansi.YELLOW)
}
} catch (GrgitException exception) {
println color("Unable to update $itemName, Skipping: ${exception.getMessage()}", Ansi.RED)
}
Expand Down
97 changes: 50 additions & 47 deletions config/groovy/util.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ switch (cleanerArgs[0]) {
case "update-all":
println "We're updating every $itemType"
println "List of local entries: ${common.retrieveLocalItems()}"
def skipRecentlyUpdated = cleanerArgs.contains("-skip-recently-updated")
for (item in common.retrieveLocalItems()) {
common.updateItem(item)
common.updateItem(item, skipRecentlyUpdated)
}
break

Expand Down Expand Up @@ -297,50 +298,52 @@ private void printListItemsCondensed(String[] items) {
* Simply prints usage information.
*/
def printUsage() {
println ""
println "Utility script for interacting with Terasology. General syntax:"
println " groovyw (type) (sub-command)"
println "- 'type' may be module,meta,lib or facade."
println ""
println "Available sub-commands:"
println "- 'init' - retrieves a given module distro, or a default sample source module (modules only)"
println "- 'get' - retrieves one or more items in source form (separate with spaces)"
println "- 'get-all' - retrieves all modules that can be found on the configured remote locations"
println "- 'recurse' - retrieves the given item(s) *and* their dependencies in source form (really only for modules)"
println "- 'list' - lists items that are available for download or downloaded already."
println "- 'create' - creates a new item of the given type."
println "- 'update' - updates an item (git pulls latest from current origin, if workspace is clean"
println "- 'update-all' - updates all local items of the given type."
println "- 'add-remote (item) (name)' - adds a remote (name) to (item) with the default URL."
println "- 'add-remote (item) (name) (URL)' - adds a remote with the given URL"
println "- 'list-remotes (item)' - lists all remotes for (item) "
println "- 'refresh' - replaces the Gradle build file for all items of the given type from the latest template"
println "- 'createDependencyDotFile' - creates a dot file recursively listing dependencies of given locally available module, can be visualized with e.g. graphviz"
println ""
println "Available flags:"
println "'-remote [someRemote]' to clone from an alternative remote, also adding the upstream org (like MovingBlocks) repo as 'origin'"
println " Note: 'get' + 'recurse' only. This will override an alternativeGithubHome set via gradle.properties."
println "'-simple-list-format' to print one item per row for the 'list' sub-command, even for large numbers of items"
println "'-condensed-list-format' to group items by starting letter for the 'list' sub-command (default with many items)"
println ""
println "Example: 'groovyw module init iota' - retrieves all the modules in the Iota module distro from GitHub."
println "Example: 'groovyw module get Sample -remote jellysnake' - would retrieve Sample from jellysnake's Sample repo on GitHub."
println "Example: 'groovyw module get-all' - would retrieve all the modules in the Terasology organisation on GitHub."
println "Example: 'groovyw module get Sa??l*' - would retrieve all the modules in the Terasology organisation on GitHub" +
" that start with \"Sa\", have any two characters after that, then an \"l\" and then end with anything else." +
" This should retrieve the Sample repository from the Terasology organisation on GitHub."
println ""
println "*NOTE*: On UNIX platforms (MacOS and Linux), the wildcard arguments must be escaped with single quotes e.g. groovyw module get '*'."
println ""
println "Example: 'groovyw module recurse GooeysQuests Sample' - would retrieve those modules plus their dependencies as source"
println "Example: 'groovyw lib list' - would list library projects compatible with being embedded in a Terasology workspace"
println "Example: 'groovyw module createDependencyDotFile JoshariasSurvival' - would create a dot file with JS' dependencies and all their dependencies - if locally available"
println ""
println "*NOTE*: Item names are case sensitive. If you add items then `gradlew idea` or similar may be needed to refresh your IDE"
println ""
println "If you omit further arguments beyond the sub-command you'll be prompted for details"
println ""
println "For advanced usage see project documentation. For instance you can provide an alternative GitHub home"
println "A gradle.properties file (one exists under '/templates' in an engine workspace) can provide such overrides"
println ""

println("""
Utility script for interacting with Terasology. General syntax:
groovyw (type) (sub-command)
- 'type' may be module,meta,lib or facade.

Available sub-commands:
- 'init' - retrieves a given module distro, or a default sample source module (modules only)
- 'get' - retrieves one or more items in source form (separate with spaces)
- 'get-all' - retrieves all modules that can be found on the configured remote locations
- 'recurse' - retrieves the given item(s) *and* their dependencies in source form (really only for modules)
- 'list' - lists items that are available for download or downloaded already.
- 'create' - creates a new item of the given type.
- 'update' - updates an item (git pulls latest from current origin, if workspace is clean
- 'update-all' - updates all local items of the given type.
- 'add-remote (item) (name)' - adds a remote (name) to (item) with the default URL.
- 'add-remote (item) (name) (URL)' - adds a remote with the given URL
- 'list-remotes (item)' - lists all remotes for (item)
- 'refresh' - replaces the Gradle build file for all items of the given type from the latest template
- 'createDependencyDotFile' - creates a dot file recursively listing dependencies of given locally available module, can be visualized with e.g. graphviz

Available flags:
'-remote [someRemote]' to clone from an alternative remote, also adding the upstream org (like MovingBlocks) repo as 'origin'
Note: 'get' + 'recurse' only. This will override an alternativeGithubHome set via gradle.properties.
'-simple-list-format' to print one item per row for the 'list' sub-command, even for large numbers of items
'-condensed-list-format' to group items by starting letter for the 'list' sub-command (default with many items)
'-skip-recently-updated' (Only for update-all) to skip updating modules that have already been updated within 10 minutes
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new line. The rest is removing unnecessary println statements in favor of a multi-line string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear, I meant line 327


Example: 'groovyw module init iota' - retrieves all the modules in the Iota module distro from GitHub.
Example: 'groovyw module get Sample -remote jellysnake' - would retrieve Sample from jellysnake's Sample repo on GitHub.
Example: 'groovyw module get-all' - would retrieve all the modules in the Terasology organisation on GitHub.
Example: 'groovyw module get Sa??l*' - would retrieve all the modules in the Terasology organisation on GitHub"
that start with \"Sa\", have any two characters after that, then an \"l\" and then end with anything else."
This should retrieve the Sample repository from the Terasology organisation on GitHub.

*NOTE*: On UNIX platforms (MacOS and Linux), the wildcard arguments must be escaped with single quotes e.g. groovyw module get '*'.

Example: 'groovyw module recurse GooeysQuests Sample' - would retrieve those modules plus their dependencies as source
Example: 'groovyw lib list' - would list library projects compatible with being embedded in a Terasology workspace
Example: 'groovyw module createDependencyDotFile JoshariasSurvival' - would create a dot file with JS' dependencies and all their dependencies - if locally available

*NOTE*: Item names are case sensitive. If you add items then `gradlew idea` or similar may be needed to refresh your IDE

If you omit further arguments beyond the sub-command you'll be prompted for details

For advanced usage see project documentation. For instance you can provide an alternative GitHub home
A gradle.properties file (one exists under '/templates' in an engine workspace) can provide such overrides
""".stripIndent())
}