Skip to content

Commit e322c36

Browse files
committed
add "outdated" command to check deps
1 parent 06e82ef commit e322c36

5 files changed

Lines changed: 71 additions & 1 deletion

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ The command exits with code `0` when compilation succeeds and `1` on compilation
6060
> grill typecheck
6161
```
6262

63+
### Checking dependency updates
64+
65+
Use `outdated` to check whether any dependency is not on the latest commit of its configured branch
66+
(or repository default branch when none is specified).
67+
The command exits with code `0` when dependencies are up to date and `1` when updates are available.
68+
69+
```cmd
70+
> grill outdated
71+
```
72+
6373

6474
### Building the project
6575

src/main/kotlin/file/CLICommand.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum class CLICommand {
77
GENERATE,
88
TEST,
99
TYPECHECK,
10+
OUTDATED,
1011
BUILD,
1112
SELF_UPDATE
1213
}

src/main/kotlin/file/DependencyManager.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ object DependencyManager {
8585
return false
8686
}
8787

88+
fun hasOutdatedDependencies(projectRoot: Path, projectConfig: WurstProjectConfigData): Boolean {
89+
Log.print("Checking dependencies...\n")
90+
for (dependency in projectConfig.dependencies) {
91+
val (depUri, dependencyName, requestedBranch) = resolveName(dependency)
92+
val branch = resolveBranch(depUri, requestedBranch)
93+
Log.print("Checking dependency - $dependencyName ..")
94+
val depFolder = projectRoot.resolve("_build/dependencies/$dependencyName")
95+
96+
if (!Files.exists(depFolder.resolve(".git"))) {
97+
Log.print("missing\n")
98+
return true
99+
}
100+
101+
if (isDependencyOutdated(depFolder, depUri, branch)) {
102+
Log.print("outdated\n")
103+
return true
104+
}
105+
Log.print("ok\n")
106+
}
107+
return false
108+
}
109+
88110
fun cloneRepo(dependency: String, depFolder: Path) {
89111
val (depURI, _, requestedBranch) = resolveName(dependency)
90112
val branch = resolveBranch(depURI, requestedBranch)
@@ -264,4 +286,25 @@ object DependencyManager {
264286
}
265287
return false
266288
}
289+
290+
private fun isDependencyOutdated(depFolder: Path, depUri: String, branch: String): Boolean {
291+
return try {
292+
FileRepository(depFolder.resolve(".git").toFile()).use { repository ->
293+
repository.config.setString("remote", "origin", "url", depUri)
294+
repository.config.save()
295+
Git(repository).use { git ->
296+
git.fetch()
297+
.setRemote("origin")
298+
.setRemoveDeletedRefs(true)
299+
.call()
300+
}
301+
val localHead = repository.resolve(Constants.HEAD)
302+
val remoteHead = repository.resolve("refs/remotes/origin/$branch")
303+
localHead == null || remoteHead == null || localHead != remoteHead
304+
}
305+
} catch (e: Exception) {
306+
log.warn("Could not verify dependency at <$depFolder>.", e)
307+
true
308+
}
309+
}
267310
}

src/main/kotlin/file/SetupApp.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ object SetupApp {
118118
typecheckProject(configData)
119119
}
120120
}
121+
setup.command == CLICommand.OUTDATED -> {
122+
if (configData == null) {
123+
log.error("No wurst.build configuration found.")
124+
exitProcess(1)
125+
}
126+
checkProjectOutdated(configData)
127+
}
121128
setup.command == CLICommand.BUILD -> {
122129
log.info("\uD83D\uDD28 Building project..")
123130
if (setup.commandArg.isBlank()) {
@@ -211,6 +218,15 @@ object SetupApp {
211218
}
212219
}
213220

221+
private fun checkProjectOutdated(configData: WurstProjectConfigData) {
222+
val outdated = DependencyManager.hasOutdatedDependencies(setup.projectRoot, configData)
223+
if (outdated) {
224+
log.info("Project dependencies are outdated. Run `grill install`.")
225+
exitProcess(1)
226+
}
227+
log.info("Project dependencies are up to date.")
228+
}
229+
214230
private fun startWurstProcess(args: ArrayList<String>): Int {
215231
val pb = ProcessBuilder(args)
216232
pb.redirectOutput(Redirect.INHERIT)

src/main/kotlin/file/SetupMain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SetupMain {
5656

5757
}
5858
} catch(e: IllegalArgumentException) {
59-
log.error("\uD83D\uDD25 Invalid grill command <$first> ! Available commands: [generate|install|remove|test|typecheck|build] <command argument>")
59+
log.error("\uD83D\uDD25 Invalid grill command <$first> ! Available commands: [generate|install|remove|test|typecheck|outdated|build] <command argument>")
6060
exitProcess(1)
6161
}
6262
}

0 commit comments

Comments
 (0)