Skip to content

Commit 51a15e1

Browse files
author
yahavi
committed
BI-465 - Support for NPM in the Jenkins Artifactory Plugin
1 parent d8d13c8 commit 51a15e1

File tree

46 files changed

+1926
-152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1926
-152
lines changed

appveyor.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
stack: jdk 8
1+
stack: jdk 8, node 8
22
skip_tags: true
33
environment:
44
matrix:
@@ -16,7 +16,7 @@ environment:
1616
secure: Xf371RQAxCTMn/S7NdmV8g==
1717

1818
test_script:
19-
- sh: ./gradlew clean test
20-
- cmd: gradlew.bat clean test
19+
- sh: ./gradlew clean test
20+
- cmd: gradlew.bat clean test
2121

2222
build: off

build-info-api/src/main/java/org/jfrog/build/api/Module.java

+22
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818

1919
import com.thoughtworks.xstream.annotations.XStreamAlias;
2020

21+
import java.util.Collection;
22+
import java.util.Collections;
2123
import java.util.List;
24+
import java.util.Optional;
25+
import java.util.stream.Collectors;
26+
import java.util.stream.Stream;
2227

2328
import static org.jfrog.build.api.BuildBean.MODULE;
2429

@@ -113,6 +118,23 @@ public void setExcludedArtifacts(List<Artifact> excludedArtifacts) {
113118
this.excludedArtifacts = excludedArtifacts;
114119
}
115120

121+
/**
122+
* Append other module to this module
123+
*
124+
* @param other Module to append
125+
*/
126+
public void append(Module other) {
127+
artifacts = appendBuildFileLists(artifacts, other.getArtifacts());
128+
excludedArtifacts = appendBuildFileLists(excludedArtifacts, other.getExcludedArtifacts());
129+
dependencies = appendBuildFileLists(dependencies, other.getDependencies());
130+
}
131+
132+
private <T extends BaseBuildBean> List<T> appendBuildFileLists(List<T> a, List<T> b) {
133+
return Stream.of(Optional.ofNullable(a).orElseGet(Collections::emptyList), Optional.ofNullable(b).orElseGet(Collections::emptyList))
134+
.flatMap(Collection::stream)
135+
.collect(Collectors.toList());
136+
}
137+
116138
@Override
117139
public boolean equals(Object o) {
118140
if (this == o) return true;

build-info-api/src/main/java/org/jfrog/build/api/builder/DependencyBuilder.java

+15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.collect.Lists;
2020
import org.jfrog.build.api.Dependency;
2121

22+
import java.util.HashSet;
2223
import java.util.List;
2324
import java.util.Properties;
2425
import java.util.Set;
@@ -90,6 +91,20 @@ public DependencyBuilder scopes(Set<String> scopes) {
9091
return this;
9192
}
9293

94+
/**
95+
* Adds the given scope to the scopes set
96+
*
97+
* @param scope Dependency scope list
98+
* @return Builder instance
99+
*/
100+
public DependencyBuilder addScope(String scope) {
101+
if (scopes == null) {
102+
scopes = new HashSet<>();
103+
}
104+
scopes.add(scope);
105+
return this;
106+
}
107+
93108
/**
94109
* Sets the SHA1 checksum of the dependency
95110
*

build-info-extractor/src/main/java/org/jfrog/build/extractor/producerConsumer/ProducerConsumerItem.java build-info-api/src/main/java/org/jfrog/build/api/producerConsumer/ProducerConsumerItem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.jfrog.build.extractor.producerConsumer;
1+
package org.jfrog.build.api.producerConsumer;
22

33
/**
44
* Interface for defining a ProducerConsumerExecutor suitable item

build-info-extractor-gradle/src/main/groovy/org/jfrog/gradle/plugin/artifactory/extractor/GradleBuildInfoExtractor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
*
6262
* @author Tomer Cohen
6363
*/
64-
public class GradleBuildInfoExtractor implements BuildInfoExtractor<Project, Build> {
64+
public class GradleBuildInfoExtractor implements BuildInfoExtractor<Project> {
6565
private static final Logger log = Logging.getLogger(GradleBuildInfoExtractor.class);
6666

6767
private static final String SHA1 = "sha1";

build-info-extractor-maven3/src/main/java/org/jfrog/build/extractor/maven/BuildInfoRecorder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
* @author Noam Y. Tenne
7474
*/
7575
@Component(role = BuildInfoRecorder.class)
76-
public class BuildInfoRecorder extends AbstractExecutionListener implements BuildInfoExtractor<ExecutionEvent, Build> {
76+
public class BuildInfoRecorder extends AbstractExecutionListener implements BuildInfoExtractor<ExecutionEvent> {
7777

7878
@Requirement
7979
private Logger logger;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.jfrog.build.extractor.npm;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.ObjectReader;
6+
import org.apache.commons.lang.StringUtils;
7+
import org.jfrog.build.extractor.executor.CommandExecutor;
8+
import org.jfrog.build.extractor.executor.CommandResults;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.io.Serializable;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
import java.util.stream.Stream;
19+
20+
/**
21+
* @author Yahav Itzhak
22+
*/
23+
public class NpmDriver implements Serializable {
24+
private static final long serialVersionUID = 1L;
25+
26+
private static ObjectReader jsonReader = new ObjectMapper().reader();
27+
private CommandExecutor commandExecutor;
28+
29+
public NpmDriver(String executablePath) {
30+
this.commandExecutor = new CommandExecutor(StringUtils.defaultIfEmpty(executablePath, "npm"));
31+
}
32+
33+
@SuppressWarnings("unused")
34+
public boolean isNpmInstalled() {
35+
try {
36+
version(new File(""));
37+
return true;
38+
} catch (IOException | InterruptedException e) {
39+
return false;
40+
}
41+
}
42+
43+
public void install(File workingDirectory, List<String> extraArgs) throws IOException {
44+
try {
45+
runCommand(workingDirectory, new String[]{"i"}, extraArgs);
46+
} catch (IOException | InterruptedException e) {
47+
throw new IOException("npm install failed: " + e.getMessage(), e);
48+
}
49+
}
50+
51+
public void pack(File workingDirectory, List<String> extraArgs) throws IOException {
52+
try {
53+
runCommand(workingDirectory, new String[]{"pack"}, extraArgs);
54+
} catch (IOException | InterruptedException e) {
55+
throw new IOException("npm pack failed: " + e.getMessage(), e);
56+
}
57+
}
58+
59+
public JsonNode list(File workingDirectory, List<String> extraArgs) throws IOException {
60+
List<String> args = new ArrayList<>();
61+
args.add("ls");
62+
args.add("--json");
63+
args.addAll(extraArgs);
64+
try {
65+
CommandResults npmCommandRes = commandExecutor.exeCommand(workingDirectory, args);
66+
String res = StringUtils.isBlank(npmCommandRes.getRes()) ? "{}" : npmCommandRes.getRes();
67+
return jsonReader.readTree(res);
68+
} catch (IOException | InterruptedException e) {
69+
throw new IOException("npm ls failed", e);
70+
}
71+
}
72+
73+
public String version(File workingDirectory) throws IOException, InterruptedException {
74+
return runCommand(workingDirectory, new String[]{"--version"}, Collections.emptyList());
75+
}
76+
77+
public String configList(File workingDirectory, List<String> extraArgs) throws IOException, InterruptedException {
78+
return runCommand(workingDirectory, new String[]{"c", "ls", "--json"}, extraArgs);
79+
}
80+
81+
private String runCommand(File workingDirectory, String[] args, List<String> extraArgs) throws IOException, InterruptedException {
82+
List<String> finalArgs = Stream.concat(Arrays.stream(args), extraArgs.stream()).collect(Collectors.toList());
83+
CommandResults npmCommandRes = commandExecutor.exeCommand(workingDirectory, finalArgs);
84+
if (!npmCommandRes.isOk()) {
85+
throw new IOException(npmCommandRes.getErr());
86+
}
87+
return npmCommandRes.getRes();
88+
}
89+
}

0 commit comments

Comments
 (0)