Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
d3vis
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marcel Steinbeck
d3vis
Commits
c3fd7091
Commit
c3fd7091
authored
6 years ago
by
Marcel Steinbeck
Browse files
Options
Downloads
Patches
Plain Diff
Use Spoon to detect imports.
parent
ea93878c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dependencies/dependencies.groovy
+28
-29
28 additions, 29 deletions
dependencies/dependencies.groovy
with
28 additions
and
29 deletions
dependencies/dependencies.groovy
+
28
−
29
View file @
c3fd7091
///////////////////////////// Dependencies
@Grapes
([
@Grab
(
group
=
'com.github.javaparser'
,
module
=
'javaparser-core'
,
version
=
'3.6.7'
)]
)
import
com.github.javaparser.JavaParser
import
groovy.io.FileType
@Grab
(
group
=
'fr.inria.gforge.spoon'
,
module
=
'spoon-core'
,
version
=
'7.0.0'
)
])
import
groovy.transform.Field
import
spoon.*
import
spoon.reflect.declaration.*
import
spoon.reflect.visitor.*
import
java.nio.charset.StandardCharsets
import
java.nio.file.Files
import
java.nio.file.Path
import
java.nio.file.Paths
import
java.util.stream.Collectors
import
static
java
.
lang
.
System
.
exit
import
static
java
.
lang
.
System
.
exit
///////////////////////////// Configuration
@Field
final
SCRIPT_LOCATION
=
new
File
(
this
.
class
.
protectionDomain
.
codeSource
.
location
.
path
).
parent
...
...
@@ -75,31 +74,31 @@ padding = Math.max(padding as int, 150)
///////////////////////////// Generate JSON string
Optional
<
String
>
parseJavaFile
(
Path
p
)
{
String
ex
=
exclude
def
cu
=
JavaParser
.
parse
(
p
)
def
pkg
=
cu
.
packageDeclaration
.
map
{
pd
->
pd
.
nameAsString
}
.
orElse
(
''
)
def
base
=
p
.
fileName
.
toString
().
replaceFirst
(
'[.][^.]+$'
,
''
)
def
name
=
"${pkg}.${base}"
if
(
name
.
matches
(
ex
))
{
return
Optional
.
empty
()
def
entries
=
[]
def
launcher
=
new
Launcher
();
launcher
.
addInputResource
(
input
)
launcher
.
environment
.
noClasspath
=
true
launcher
.
buildModel
()
launcher
.
factory
.
CompilationUnit
().
map
.
values
().
each
{
def
name
=
it
.
declaredPackage
.
qualifiedName
+
"."
+
it
.
file
.
name
.
replaceFirst
(
'[.][^.]+$'
,
''
)
if
(
name
.
matches
((
String
)
exclude
))
{
return
}
def
imports
=
new
HashSet
<
CtImport
>()
it
.
declaredTypes
.
each
{
ImportScanner
scanner
=
new
ImportScannerImpl
()
scanner
.
scan
(
it
)
imports
.
addAll
(
scanner
.
getAllImports
())
}
def
imports
=
String
.
join
(
','
,
cu
.
imports
.
stream
()
.
map
{
i
->
i
.
nameAsString
}
.
filter
{
i
->
!
i
.
matches
(
ex
)}
def
importsStr
=
String
.
join
(
','
,
imports
.
stream
()
.
map
{
i
->
i
.
reference
}
.
map
{
i
->
i
.
toString
()}
.
filter
{
i
->
!
i
.
isEmpty
()}
.
map
{
i
->
"\"${i}\""
}
.
collect
(
Collectors
.
toList
()))
return
Optional
.
of
(
'{"name":"'
+
name
+
'","size":1,"imports":['
+
imports
+
']}'
)
}
def
entries
=
[]
new
File
(
input
).
eachFileRecurse
(
FileType
.
FILES
)
{
if
(
it
.
name
.
endsWith
(
".java"
))
{
parseJavaFile
(
it
.
toPath
()).
ifPresent
({
e
->
entries
<<
e
})
}
entries
<<
'{"name":"'
+
name
+
'","size":1,"imports":['
+
importsStr
+
']}'
}
def
json
=
'['
+
String
.
join
(
','
,
entries
)
+
']'
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment