Skip to content

Commit 96c76e4

Browse files
fix: java output location moved to src/main (#3)
None, but related to comments on this PR: cdk8s-team/cdk8s#233 (comment) *Description of changes:* Rather than putting imports into a deep folder structure with package name, they all fall under `src/main`. A `cdk8s` java project with multiple imports looks like: ``` imports/ src/ main/ java/ k8s/ mattermost/ resources/ k8s/ mattermost/ ``` Note: using [ncp](https://github.com/AvianFlu/ncp) because `fs-extra` doesn't have copy/merge functionality. With `fs.move` option `overwrite` as `true`, you only get the last import in your list of imports. With `overwrite` false, it fails because `imports/src/main/` already exists. We want to effectively merge the two imports into this folder. `ncp` seemed like the best option here for this. Tested by `yarn link`ing this with `cdk8s` and importing over there. Signed-off-by: campionfellin <campionfellin@gmail.com>
1 parent 8cc9ff2 commit 96c76e4

7 files changed

Lines changed: 319 additions & 308 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@v2
10-
- run: npx projen@0.1.17
11-
- run: yarn install --frozen-lockfile
10+
- run: npx projen@0.1.19
11+
- run: yarn install
1212
- run: yarn build

.projenrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ const project = new TypeScriptLibraryProject({
1212
'jsii-srcmak': 'bin/jsii-srcmak'
1313
},
1414
devDependencies: {
15+
'@types/ncp': Semver.caret('2.0.4'),
1516
'@types/node': Semver.caret('13.9.8'),
1617
'@types/fs-extra': Semver.caret('9.0.1'),
1718
},
1819
dependencies: {
1920
'jsii': Semver.caret('1.5.0'),
2021
'jsii-pacmak': Semver.caret('1.5.0'),
2122
'fs-extra': Semver.caret('9.0.0'),
23+
'ncp': Semver.caret('2.0.0'),
2224
'yargs': Semver.caret('11.1.1'),
2325
},
2426
releaseToNpm: true

lib/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface Options {
2525
python?: PythonOutputOptions;
2626

2727
/**
28-
* Produces java code under src/main/yourpackage/main/java/yourpackage/
28+
* Produces java code under src/main/
2929
*
3030
* @default - java is not generated
3131
*/

lib/srcmak.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from 'path';
33
import { exec, mkdtemp } from './util';
44
import { compile } from './compile';
55
import { Options } from './options';
6+
import { ncp } from 'ncp';
67

78
const pacmakModule = require.resolve('jsii-pacmak/bin/jsii-pacmak');
89

@@ -35,10 +36,13 @@ export async function srcmak(srcdir: string, options: Options = { }) {
3536
}
3637

3738
if (options.java) {
38-
const reldir = options.java.package.replace(/\./g, '/');
3939
const source = path.resolve(path.join(workdir, 'dist/java/src/'));
40-
const target = path.join(options.java.outdir, 'src/main', reldir);
41-
await fs.move(source, target, { overwrite: true });
40+
const target = path.join(options.java.outdir, 'src/');
41+
ncp(source, target, { clobber: false }, (err) => {
42+
if (err) {
43+
return console.error(err);
44+
}
45+
});
4246
}
4347
});
4448
}

test/__snapshots__/cli.test.js.snap

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`java output 1`] = `
44
Object {
5-
"src/main/mypackage/main/java/mypackage/$Module.java": "package mypackage;
5+
"src/main/java/mypackage/$Module.java": "package mypackage;
66
77
import java.io.BufferedReader;
88
import java.io.InputStream;
@@ -53,7 +53,11 @@ public final class $Module extends JsiiModule {
5353
if (!MODULE_TYPES.containsKey(fqn)) {
5454
throw new ClassNotFoundException(\\"Unknown JSII type: \\" + fqn);
5555
}
56-
return this.cache.computeIfAbsent(MODULE_TYPES.get(fqn), this::findClass);
56+
String className = MODULE_TYPES.get(fqn);
57+
if (!this.cache.containsKey(className)) {
58+
this.cache.put(className, this.findClass(className));
59+
}
60+
return this.cache.get(className);
5761
}
5862
5963
private Class<?> findClass(final String binaryName) {
@@ -66,7 +70,7 @@ public final class $Module extends JsiiModule {
6670
}
6771
}
6872
",
69-
"src/main/mypackage/main/java/mypackage/Calculator.java": "package mypackage;
73+
"src/main/java/mypackage/Calculator.java": "package mypackage;
7074
7175
/**
7276
* A sophisticaed multi-language calculator.
@@ -116,7 +120,7 @@ public class Calculator extends software.amazon.jsii.JsiiObject {
116120
}
117121
}
118122
",
119-
"src/main/mypackage/main/java/mypackage/Operands.java": "package mypackage;
123+
"src/main/java/mypackage/Operands.java": "package mypackage;
120124
121125
/**
122126
* Math operands.
@@ -145,7 +149,7 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
145149
/**
146150
* A builder for {@link Operands}
147151
*/
148-
public static final class Builder {
152+
public static final class Builder implements software.amazon.jsii.Builder<Operands> {
149153
private java.lang.Number lhs;
150154
private java.lang.Number rhs;
151155
@@ -174,6 +178,7 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
174178
* @return a new instance of {@link Operands}
175179
* @throws NullPointerException if any required attribute was not provided
176180
*/
181+
@Override
177182
public Operands build() {
178183
return new Jsii$Proxy(lhs, rhs);
179184
}
@@ -253,7 +258,7 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
253258
}
254259
}
255260
",
256-
"src/main/mypackage/main/resources/mypackage/$Module.txt": "generated.Calculator=mypackage.Calculator
261+
"src/main/resources/mypackage/$Module.txt": "generated.Calculator=mypackage.Calculator
257262
generated.Operands=mypackage.Operands
258263
",
259264
}
@@ -268,9 +273,9 @@ Object {
268273
],
269274
},
270275
"description": "generated",
271-
"fingerprint": "YzbxW50rbVtKyO3xyXOGChedDt8pdGdCRYTETM1PW7k=",
276+
"fingerprint": "+fSgo9PQwFAKmX2JcaRILwTXE/p1H1bq+QKuOREAfuE=",
272277
"homepage": "http://generated",
273-
"jsiiVersion": "1.5.0 (build 46538f8)",
278+
"jsiiVersion": "1.6.0 (build 248e75b)",
274279
"license": "Apache-2.0",
275280
"name": "generated",
276281
"repository": Object {

test/__snapshots__/srcmak.test.js.snap

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`java + different entrypoint 1`] = `
44
Object {
5-
"src/main/hello/world/main/java/hello/world/$Module.java": "package hello.world;
5+
"src/main/java/hello/world/$Module.java": "package hello.world;
66
77
import java.io.BufferedReader;
88
import java.io.InputStream;
@@ -53,7 +53,11 @@ public final class $Module extends JsiiModule {
5353
if (!MODULE_TYPES.containsKey(fqn)) {
5454
throw new ClassNotFoundException(\\"Unknown JSII type: \\" + fqn);
5555
}
56-
return this.cache.computeIfAbsent(MODULE_TYPES.get(fqn), this::findClass);
56+
String className = MODULE_TYPES.get(fqn);
57+
if (!this.cache.containsKey(className)) {
58+
this.cache.put(className, this.findClass(className));
59+
}
60+
return this.cache.get(className);
5761
}
5862
5963
private Class<?> findClass(final String binaryName) {
@@ -66,7 +70,7 @@ public final class $Module extends JsiiModule {
6670
}
6771
}
6872
",
69-
"src/main/hello/world/main/java/hello/world/Hello.java": "package hello.world;
73+
"src/main/java/hello/world/Hello.java": "package hello.world;
7074
7175
7276
@software.amazon.jsii.Jsii(module = hello.world.$Module.class, fqn = \\"generated.Hello\\")
@@ -90,7 +94,7 @@ public class Hello extends software.amazon.jsii.JsiiObject {
9094
}
9195
}
9296
",
93-
"src/main/hello/world/main/java/hello/world/Operands.java": "package hello.world;
97+
"src/main/java/hello/world/Operands.java": "package hello.world;
9498
9599
96100
@software.amazon.jsii.Jsii(module = hello.world.$Module.class, fqn = \\"generated.Operands\\")
@@ -110,7 +114,7 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
110114
/**
111115
* A builder for {@link Operands}
112116
*/
113-
public static final class Builder {
117+
public static final class Builder implements software.amazon.jsii.Builder<Operands> {
114118
private java.lang.Number lhs;
115119
private java.lang.Number rhs;
116120
@@ -139,6 +143,7 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
139143
* @return a new instance of {@link Operands}
140144
* @throws NullPointerException if any required attribute was not provided
141145
*/
146+
@Override
142147
public Operands build() {
143148
return new Jsii$Proxy(lhs, rhs);
144149
}
@@ -218,7 +223,7 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
218223
}
219224
}
220225
",
221-
"src/main/hello/world/main/resources/hello/world/$Module.txt": "generated.Hello=hello.world.Hello
226+
"src/main/resources/hello/world/$Module.txt": "generated.Hello=hello.world.Hello
222227
generated.Operands=hello.world.Operands
223228
",
224229
}
@@ -233,9 +238,9 @@ Object {
233238
],
234239
},
235240
"description": "generated",
236-
"fingerprint": "mDN1As+e+zwuytAHqC8o+7gwTrnrzC2d65pSGEUAe1I=",
241+
"fingerprint": "cNtb65kyZQOiBulmzr+bs6dCGdv4uhea1tFTkbRGJTw=",
237242
"homepage": "http://generated",
238-
"jsiiVersion": "1.5.0 (build 46538f8)",
243+
"jsiiVersion": "1.6.0 (build 248e75b)",
239244
"license": "Apache-2.0",
240245
"name": "generated",
241246
"repository": Object {

0 commit comments

Comments
 (0)