Skip to content

Commit

Permalink
Only print transitive dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
doru1004 committed Nov 2, 2020
1 parent b397ca6 commit f94f485
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 69 deletions.
6 changes: 3 additions & 3 deletions pkg/builder/runtime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func computeDependencies(ctx *builder.Context) error {

// Process artifacts list and add it to existing artifacts.
artifacts := []v1.Artifact{}
artifacts, err = ProcessTransitiveDependencies(content, "dependencies")
artifacts, err = ProcessTransitiveDependencies(content)
if err != nil {
return err
}
Expand All @@ -136,7 +136,7 @@ func ComputeDependenciesCommon(mc maven.Context, runtimeVersion string) ([]byte,
}

// ProcessTransitiveDependencies --
func ProcessTransitiveDependencies(content []byte, outputDir string) ([]v1.Artifact, error) {
func ProcessTransitiveDependencies(content []byte) ([]v1.Artifact, error) {
cp := make(map[string][]v1.Artifact)
err := yaml2.Unmarshal(content, &cp)
if err != nil {
Expand Down Expand Up @@ -167,7 +167,7 @@ func ProcessTransitiveDependencies(content []byte, outputDir string) ([]v1.Artif
artifacts = append(artifacts, v1.Artifact{
ID: e.ID,
Location: e.Location,
Target: path.Join(outputDir, gav.GroupID+"."+fileName),
Target: path.Join("dependencies", gav.GroupID+"."+fileName),
Checksum: e.Checksum,
})
}
Expand Down
77 changes: 11 additions & 66 deletions pkg/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ will be generated by calling Maven and then copied into the directory pointed to
if err := options.validate(args); err != nil {
return err
}
if err := options.initialize(args); err != nil {
return err
}
if err := options.run(args); err != nil {
fmt.Println(err.Error())
}
Expand All @@ -75,7 +72,6 @@ will be generated by calling Maven and then copied into the directory pointed to
cmd.Flags().StringArrayP("dependency", "d", nil, `Additional top-level dependency with the format:
<type>:<dependency-name>
where <type> is one of {`+strings.Join(acceptedDependencyTypes, "|")+`}.`)
cmd.Flags().String("dependencies-directory", "", "Directory that will contain all the computed dependencies. Default: <kamel-invocation-directory>/dependencies")
cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml")

return &cmd, &options
Expand All @@ -85,7 +81,6 @@ type inspectCmdOptions struct {
*RootCmdOptions
AllDependencies bool `mapstructure:"all-dependencies"`
OutputFormat string `mapstructure:"output"`
DependenciesDirectory string `mapstructure:"dependencies-directory"`
AdditionalDependencies []string `mapstructure:"dependencies"`
}

Expand Down Expand Up @@ -131,33 +126,6 @@ func (command *inspectCmdOptions) validate(args []string) error {
}
}

// If provided, ensure that that the dependencies directory exists.
if command.DependenciesDirectory != "" {
dependenciesDirectoryExists, err := util.DirectoryExists(command.DependenciesDirectory)
// Report any error.
if err != nil {
return err
}

// Signal file not found.
if !dependenciesDirectoryExists {
return errors.New("input file " + command.DependenciesDirectory + " file does not exist")
}
}

return nil
}

func (command *inspectCmdOptions) initialize(args []string) error {
// If --all-dependencies flag is set the dependencies directory needs to have a valid value.
// If not provided on the command line, the value needs to be initialized with the default.
if command.AllDependencies {
// Move the integration dependecies to the dependencies directory.
err := createAndSetDependenciesDirectory(command)
if err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -191,7 +159,7 @@ func (command *inspectCmdOptions) run(args []string) error {
return nil
}

func getTopLevelDependencies(catalog *camel.RuntimeCatalog, format string, args []string, outputPlainText bool) ([]string, error) {
func getTopLevelDependencies(catalog *camel.RuntimeCatalog, format string, args []string, printDependencies bool) ([]string, error) {
// List of top-level dependencies.
dependencies := strset.New()

Expand All @@ -214,9 +182,11 @@ func getTopLevelDependencies(catalog *camel.RuntimeCatalog, format string, args
dependencies.Merge(trait.AddSourceDependencies(sourceSpec, catalog))
}

err := outputDependencies(dependencies.List(), format, outputPlainText)
if err != nil {
return []string{}, err
if printDependencies {
err := outputDependencies(dependencies.List(), format)
if err != nil {
return []string{}, err
}
}

return dependencies.List(), nil
Expand Down Expand Up @@ -277,42 +247,36 @@ func getTransitiveDependencies(

// Compose artifacts list.
artifacts := []v1.Artifact{}
artifacts, err = runtime.ProcessTransitiveDependencies(content, command.DependenciesDirectory)
artifacts, err = runtime.ProcessTransitiveDependencies(content)
if err != nil {
return err
}

// Dump dependencies in the dependencies directory and construct the list of dependencies.
transitiveDependencies := []string{}
for _, entry := range artifacts {
// Copy dependencies from Maven default directory to the DependenciesDirectory.
_, err := util.CopyFile(entry.Location, entry.Target)
if err != nil {
return err
}

transitiveDependencies = append(transitiveDependencies, entry.Target)
transitiveDependencies = append(transitiveDependencies, entry.Location)
}

// Remove directory used for computing the dependencies.
defer os.RemoveAll(temporaryDirectory)

// Output transitive dependencies only if requested via the output format flag.
err = outputDependencies(transitiveDependencies, command.OutputFormat, false)
err = outputDependencies(transitiveDependencies, command.OutputFormat)
if err != nil {
return err
}

return nil
}

func outputDependencies(dependencies []string, format string, outputPlainText bool) error {
func outputDependencies(dependencies []string, format string) error {
if format != "" {
err := printDependencies(format, dependencies)
if err != nil {
return err
}
} else if outputPlainText {
} else {
// Print output in text form.
for _, dep := range dependencies {
fmt.Printf("%v\n", dep)
Expand Down Expand Up @@ -351,25 +315,6 @@ func getWorkingDirectory() (string, error) {
return currentDirectory, nil
}

func createAndSetDependenciesDirectory(command *inspectCmdOptions) error {
if command.DependenciesDirectory == "" {
currentDirectory, err := getWorkingDirectory()
if err != nil {
return err
}

command.DependenciesDirectory = path.Join(currentDirectory, defaultDependenciesDirectoryName)
}

// Create the dependencies directory if it does not already exist.
err := util.CreateDirectory(command.DependenciesDirectory)
if err != nil {
return err
}

return nil
}

func createCamelCatalog() (*camel.RuntimeCatalog, error) {
// Attempt to reuse existing Camel catalog if one is present.
catalog, err := camel.MainCatalog()
Expand Down

0 comments on commit f94f485

Please sign in to comment.