• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moosetechnology / MooseNexus / 12725447942

11 Jan 2025 03:30PM UTC coverage: 36.509% (+1.6%) from 34.906%
12725447942

push

github

Gabriel-Darbord
Remove dead test class

525 of 1438 relevant lines covered (36.51%)

1.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/MooseNexus/NexusGradleProjectImporter.class.st
1
"
2
I am responsible for importing a project that uses Gradle to manage dependencies.
3
Gradle has the `dependencies` option to print the full dependency tree, which contains redundancies and is difficult to parse.
4
However, it also allows the definition of custom tasks, which are used to efficiently obtain the project description.
5
"
6
Class {
7
        #name : 'NexusGradleProjectImporter',
8
        #superclass : 'NexusManagedProjectImporter',
9
        #category : 'MooseNexus-Importer',
10
        #package : 'MooseNexus',
11
        #tag : 'Importer'
12
}
13

14
{ #category : 'testing' }
15
NexusGradleProjectImporter class >> canHandle: directory [
×
16

×
17
        | directoryReference |
×
18
        directoryReference := directory asFileReference.
×
19
        directoryReference / 'build.gradle' ifExists: [ ^ true ].
×
20
        directoryReference / 'build.gradle.kts' ifExists: [ ^ true ].
×
21
        ^ false
×
22
]
×
23

24
{ #category : 'constants' }
25
NexusGradleProjectImporter class >> groovyCodeForDependencies [
×
26
        "Code to insert in a build.gradle file to get flat list of dependencies."
×
27

×
28
        ^ 'tasks.register("' , self taskName , '") {
×
29
        doLast {
×
30
                def rootDirPath = rootDir.canonicalPath
×
31
                allprojects.findAll { it.projectDir.canonicalPath.startsWith(rootDirPath) }.each { project ->
×
32
                        println "-${project.group}:${project.name}:${project.version}"
×
33
                        def projectPath = project.projectDir.canonicalPath
×
34
                        if (projectPath.equals(rootDirPath)) {
×
35
                                println "."
×
36
                        } else {
×
37
                                println(projectDirPath.substring(rootDirPath.length() + 1))
×
38
                        }
×
39
                        configurations.findAll { it.canBeResolved }.each { configuration ->
×
40
                                def scope = configuration.name
×
41
                                configuration.resolvedConfiguration.resolvedArtifacts.each { artifact ->
×
42
                                        def group = artifact.moduleVersion.id.group
×
43
                                        def name = artifact.name
×
44
                                        def type = artifact.extension ?: "jar"
×
45
                                        def version = artifact.moduleVersion.id.version
×
46
                                        def path = artifact.file.absolutePath // forces artifact download
×
47
                                        println "$group:$name:$type:$version:$scope:$path"
×
48
                                }
×
49
                        }
×
50
                }
×
51
        }
×
52
}'
×
53
]
×
54

55
{ #category : 'constants' }
56
NexusGradleProjectImporter class >> kotlinCodeForDependencies [
×
57
        "Code to insert in a build.gradle.kts file to get flat list of dependencies."
×
58

×
59
        ^ 'tasks.register("' , self taskName , '") {
×
60
        doLast {
×
61
                val rootDirPath = rootDir.canonicalPath
×
62
                allprojects.filter { it.projectDir.canonicalPath.startsWith(rootDirPath) }.forEach { project ->
×
63
                        println("-${project.group}:${project.name}:${project.version}")
×
64
                        def projectPath = project.projectDir.canonicalPath
×
65
                        if (projectPath.equals(rootDirPath)) {
×
66
                                println "."
×
67
                        } else {
×
68
                                println(projectDirPath.substring(rootDirPath.length + 1))
×
69
                        }
×
70
                        configurations.filter { it.isCanBeResolved }.forEach { configuration ->
×
71
                                val scope = configuration.name
×
72
                                configuration.resolvedConfiguration.resolvedArtifacts.forEach { artifact ->
×
73
                                        val group = artifact.moduleVersion.id.group
×
74
                                        val name = artifact.name
×
75
                                        val type = artifact.extension ?: "jar"
×
76
                                        val version = artifact.moduleVersion.id.version
×
77
                                        val path = artifact.file.absolutePath // forces artifact download
×
78
                                        println("$group:$name:$type:$version:$scope:$path")
×
79
                                }
×
80
                        }
×
81
                }
×
82
        }
×
83
}'
×
84
]
×
85

86
{ #category : 'constants' }
87
NexusGradleProjectImporter class >> taskName [
×
88

×
89
        ^ 'mooseNexusListDependencies'
×
90
]
×
91

92
{ #category : 'enumerating' }
93
NexusGradleProjectImporter >> findBuildFile [
×
94

×
95
        directory / 'build.gradle' ifExists: [ :file | ^ file ].
×
96
        directory / 'build.gradle.kts' ifExists: [ :file | ^ file ].
×
97
        Error signal: 'Cannot find gradle build file'
×
98
]
×
99

100
{ #category : 'enumerating' }
101
NexusGradleProjectImporter >> findGradleCommand [
×
102
        "Return the name of the gradle executable, can be a wrapper or the global 'gradle' command."
×
103

×
104
        OSPlatform current isWindows
×
105
                ifTrue: [ directory / 'gradlew.bat' ifExists: [ ^ 'gradlew.bat' ] ]
×
106
                ifFalse: [ "assume *nix"
×
107
                directory / 'gradlew' ifExists: [ ^ './gradlew' ] ].
×
108
        "assume gradle is installed"
×
109
        ^ 'gradle'
×
110
]
×
111

112
{ #category : 'initialization' }
113
NexusGradleProjectImporter >> initialize [
×
114

×
115
        localRepositories := {
×
116
                                     NexusGradleRepository default.
×
117
                                     NexusMavenRepository default }
×
118
]
×
119

120
{ #category : 'instance creation' }
121
NexusGradleProjectImporter >> newProject [
×
122

×
123
        ^ NexusGradleProject new
×
124
]
×
125

126
{ #category : 'parsing' }
127
NexusGradleProjectImporter >> parse: raw [
×
128
        "Parses the result of the command listing the dependencies.
×
129
        Resolves each dependency to obtain the path to its local location.
×
130
        The result of the command is formatted as follows for each project:
×
131
-group:name:version
×
132
directory
×
133
(group:name:type:version:scope:path)*"
×
134

×
135
        | lines line index |
×
136
        lines := raw lines.
×
137
        index := 3. "relevant info starts at third line"
×
138

×
139
        [ (line := lines at: index) isNotEmpty ] whileTrue: [
×
140
                | group name version descriptor dependencies |
×
141
                "read project descriptor"
×
142
                descriptor := $: split: line allButFirst.
×
143
                group := descriptor first.
×
144
                name := descriptor second.
×
145
                version := descriptor third.
×
146

×
147
                "register the project with its version; project name is unique so this never overwrites"
×
148
                descriptor := Dictionary with: #version -> version.
×
149
                project projects at: group at: name put: descriptor.
×
150

×
151
                "read project location"
×
152
                descriptor
×
153
                        at: #path
×
154
                        put: ((lines at: (index := index + 1)) asFileReference relativeTo:
×
155
                                         FileLocator home) pathString.
×
156

×
157
                "read dependencies per scope"
×
158
                dependencies := Dictionary new.
×
159
                [
×
160
                (line := lines at: (index := index + 1)) isEmpty or: [
×
161
                        line first == $- ] ] whileFalse: [ "resolve the descriptor: 'group:name:type:version:scope:path'"
×
162
                        | mapping |
×
163
                        mapping := self resolve: line.
×
164
                        dependencies
×
165
                                at: mapping key
×
166
                                ifPresent: [ :descriptors | descriptors add: mapping value ]
×
167
                                ifAbsentPut: [ OrderedCollection with: mapping value ] ].
×
168

×
169
                "the current project has been fully parsed
×
170
                save the dependencies as arrays for JSON compatibility"
×
171
                descriptor at: #dependencies put: (dependencies collect: #asArray) ]
×
172
]
×
173

174
{ #category : 'reading' }
175
NexusGradleProjectImporter >> read [
×
176
        "Gradle does not provide a command to output flat dependencies.
×
177
        Instead of implementing dark magic parsing, we add a task to the build file and run it.
×
178
        Either use the global 'gradle' command or a local 'gradlew' wrapper."
×
179

×
180
        | buildFile gradleCmd originalSize |
×
181
        buildFile := self findBuildFile.
×
182
        gradleCmd := self findGradleCommand.
×
183
        originalSize := buildFile size. "to restore it later"
×
184

×
185
        ^ [ "append code to the build file for a task to extract resolved dependencies"
×
186
          buildFile writeStreamDo: [ :ws |
×
187
                  ws setToEnd crlf << (buildFile extension = 'kts'
×
188
                           ifTrue: [ self class kotlinCodeForDependencies ]
×
189
                           ifFalse: [ self class groovyCodeForDependencies ]) ].
×
190

×
191
          "execute the task we inserted and return the raw terminal output"
×
192
          LibC resultOfCommand:
×
193
                  'cd "' , directory pathString , '" && ' , gradleCmd , ' '
×
194
                  , self class taskName ] ensure: [ "restore the original build file"
×
195
                  (File named: buildFile fullName) writeStreamDo: [ :ws |
×
196
                          ws truncate: originalSize ] ]
×
197
]
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc