Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Billings committed Jun 15, 2020
2 parents c5c9f51 + 61bbf4d commit bc06e53
Show file tree
Hide file tree
Showing 37 changed files with 1,391 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.synopsys.integration.detectable.detectable.executable.resolver.GitResolver;
import com.synopsys.integration.detectable.detectable.executable.resolver.GradleResolver;
import com.synopsys.integration.detectable.detectable.executable.resolver.JavaResolver;
import com.synopsys.integration.detectable.detectable.executable.resolver.LernaResolver;
import com.synopsys.integration.detectable.detectable.executable.resolver.MavenResolver;
import com.synopsys.integration.detectable.detectable.executable.resolver.NpmResolver;
import com.synopsys.integration.detectable.detectable.executable.resolver.PearResolver;
Expand All @@ -50,7 +51,7 @@
//this will cache the find result.
public class SimpleExecutableResolver
implements GradleResolver, BashResolver, CondaResolver, CpanmResolver, CpanResolver, PearResolver, Rebar3Resolver, PythonResolver, PipResolver, PipenvResolver, MavenResolver, NpmResolver, BazelResolver, JavaResolver, DotNetResolver,
DockerResolver, GitResolver, SwiftResolver, GoResolver {
DockerResolver, GitResolver, SwiftResolver, GoResolver, LernaResolver {

private final CachedExecutableResolverOptions executableResolverOptions;
private final SimpleLocalExecutableFinder localExecutableFinder;
Expand Down Expand Up @@ -178,4 +179,9 @@ public File resolveSwift() {
public File resolveGo() {
return findCachedSystem("go");
}

@Override
public File resolveLerna() {
return findCachedSystem("lerna");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* detectable
*
* Copyright (c) 2020 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detectable.detectable.executable.resolver;

import java.io.File;

import org.jetbrains.annotations.Nullable;

import com.synopsys.integration.detectable.detectable.exception.DetectableException;

public interface LernaResolver {
@Nullable
File resolveLerna() throws DetectableException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* detectable
*
* Copyright (c) 2020 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detectable.detectables.lerna;

import java.io.File;

import com.synopsys.integration.detectable.Detectable;
import com.synopsys.integration.detectable.DetectableEnvironment;
import com.synopsys.integration.detectable.Extraction;
import com.synopsys.integration.detectable.ExtractionEnvironment;
import com.synopsys.integration.detectable.detectable.annotation.DetectableInfo;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
import com.synopsys.integration.detectable.detectable.executable.resolver.LernaResolver;
import com.synopsys.integration.detectable.detectable.file.FileFinder;
import com.synopsys.integration.detectable.detectable.result.DetectableResult;
import com.synopsys.integration.detectable.detectable.result.ExecutableNotFoundDetectableResult;
import com.synopsys.integration.detectable.detectable.result.FileNotFoundDetectableResult;
import com.synopsys.integration.detectable.detectable.result.FilesNotFoundDetectableResult;
import com.synopsys.integration.detectable.detectable.result.PassedDetectableResult;
import com.synopsys.integration.detectable.detectables.npm.lockfile.NpmPackageLockDetectable;
import com.synopsys.integration.detectable.detectables.npm.lockfile.NpmShrinkwrapDetectable;
import com.synopsys.integration.detectable.detectables.yarn.YarnLockDetectable;

@DetectableInfo(language = "Node JS", forge = "npmjs", requirementsMarkdown = "File: " + LernaDetectable.PACKAGE_JSON + ", and one of the following: " + LernaDetectable.PACKAGE_LOCK_JSON + ", " + LernaDetectable.SHRINKWRAP_JSON + ", "
+ LernaDetectable.YARN_LOCK + ".")
public class LernaDetectable extends Detectable {
public static final String LERNA_JSON = "lerna.json";
public static final String PACKAGE_JSON = NpmPackageLockDetectable.PACKAGE_JSON;
public static final String PACKAGE_LOCK_JSON = NpmPackageLockDetectable.PACKAGE_LOCK_JSON;
public static final String SHRINKWRAP_JSON = NpmShrinkwrapDetectable.SHRINKWRAP_JSON;
public static final String YARN_LOCK = YarnLockDetectable.YARN_LOCK_FILENAME;

private final FileFinder fileFinder;
private final LernaResolver lernaResolver;
private final LernaExtractor lernaExtractor;

private File lernaExecutable;

public LernaDetectable(DetectableEnvironment environment, FileFinder fileFinder, LernaResolver lernaResolver, LernaExtractor lernaExtractor) {
super(environment);
this.fileFinder = fileFinder;
this.lernaResolver = lernaResolver;
this.lernaExtractor = lernaExtractor;
}

@Override
public DetectableResult applicable() {
File lernaJsonFile = fileFinder.findFile(environment.getDirectory(), LERNA_JSON);

if (lernaJsonFile == null) {
return new FileNotFoundDetectableResult(LERNA_JSON);
}

return new PassedDetectableResult();
}

@Override
public DetectableResult extractable() throws DetectableException {
// Lerna is used in conjunction with traditional NPM projects or Yarn projects.
File packageLockFile = fileFinder.findFile(environment.getDirectory(), PACKAGE_LOCK_JSON);
File shrinkwrapFile = fileFinder.findFile(environment.getDirectory(), SHRINKWRAP_JSON);
File yarnLockFile = fileFinder.findFile(environment.getDirectory(), YARN_LOCK);
if (packageLockFile == null && shrinkwrapFile == null && yarnLockFile == null) {
return new FilesNotFoundDetectableResult(PACKAGE_LOCK_JSON, YARN_LOCK);
}

lernaExecutable = lernaResolver.resolveLerna();
if (lernaExecutable == null) {
return new ExecutableNotFoundDetectableResult("lerna");
}

return new PassedDetectableResult();
}

@Override
public Extraction extract(ExtractionEnvironment extractionEnvironment) {
return lernaExtractor.extract(environment, lernaExecutable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* detectable
*
* Copyright (c) 2020 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detectable.detectables.lerna;

import java.io.File;
import java.util.List;

import com.synopsys.integration.detectable.DetectableEnvironment;
import com.synopsys.integration.detectable.Extraction;
import com.synopsys.integration.detectable.detectables.lerna.model.LernaPackage;
import com.synopsys.integration.detectable.detectables.lerna.model.LernaResult;

public class LernaExtractor {
private final LernaPackageDiscoverer lernaPackageDiscoverer;
private final LernaPackager lernaPackager;

public LernaExtractor(LernaPackageDiscoverer lernaPackageDiscoverer, LernaPackager lernaPackager) {
this.lernaPackageDiscoverer = lernaPackageDiscoverer;
this.lernaPackager = lernaPackager;
}

public Extraction extract(DetectableEnvironment detectableEnvironment, File lernaExecutable) {
File sourceDirectory = detectableEnvironment.getDirectory();

try {
List<LernaPackage> lernaPackages = lernaPackageDiscoverer.discoverLernaPackages(sourceDirectory, lernaExecutable);
LernaResult lernaResult = lernaPackager.generateLernaResult(sourceDirectory, lernaPackages);

if (lernaResult.getException().isPresent()) {
throw lernaResult.getException().get();
}

return new Extraction.Builder()
.projectName(lernaResult.getProjectName())
.projectVersion(lernaResult.getProjectVersionName())
.success(lernaResult.getCodeLocations())
.build();
} catch (Exception e) {
return new Extraction.Builder().exception(e).build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* detectable
*
* Copyright (c) 2020 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detectable.detectables.lerna;

public class LernaOptions {
private final boolean includePrivatePackages;

public LernaOptions(boolean includePrivatePackages) {
this.includePrivatePackages = includePrivatePackages;
}

public boolean shouldIncludePrivatePackages() {
return includePrivatePackages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* detectable
*
* Copyright (c) 2020 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detectable.detectables.lerna;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.synopsys.integration.detectable.detectable.executable.ExecutableOutput;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunner;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunnerException;
import com.synopsys.integration.detectable.detectables.lerna.model.LernaPackage;

public class LernaPackageDiscoverer {
private final ExecutableRunner executableRunner;
private final Gson gson;

public LernaPackageDiscoverer(ExecutableRunner executableRunner, Gson gson) {
this.executableRunner = executableRunner;
this.gson = gson;
}

public List<LernaPackage> discoverLernaPackages(File workingDirectory, File lernaExecutable) throws ExecutableRunnerException {
ExecutableOutput lernaLsExecutableOutput = executableRunner.execute(workingDirectory, lernaExecutable, "ls", "--all", "--json");
String lernaLsOutput = lernaLsExecutableOutput.getStandardOutput();

Type lernaPackageListType = new TypeToken<ArrayList<LernaPackage>>() {}.getType();
List<LernaPackage> lernaPackages = gson.fromJson(lernaLsOutput, lernaPackageListType);

return lernaPackages.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
Loading

0 comments on commit bc06e53

Please sign in to comment.