Skip to content

Commit 7b1d38c

Browse files
authored
100 custom code snippet (#102)
* Impl custom code snippet * Update javadoc * Update ts dependencies
1 parent 881f011 commit 7b1d38c

28 files changed

Lines changed: 363 additions & 189 deletions

annotation/src/main/java/online/sharedtype/SharedType.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
* <p>
2727
* <b>Configurations:</b><br>
2828
* Global properties can be defined in a client provided property file or system properties.
29-
* By default, SharedType will look for file name "sharedtype.properties" on cmd current path.
29+
* By default, SharedType will look for file name "sharedtype.properties" on cmd path.
3030
* The property file path can be changed via compiler option "sharedtype.propsFile", see web for details.
3131
* System properties will override property file values.
32-
* Configs on class level (via this annotation) will take precedence over global properties.
32+
* Some properties are also defined at class level, i.e. via this annotation, which will take precedence over global properties.
3333
* </p>
3434
*
3535
* <p>
@@ -121,6 +121,14 @@
121121
* Type mapped this way will take the highest precedence.
122122
* E.g. a date type is configured to be emitted as string, you can override the particular mapping to emit a {@code Date}.
123123
* Type mapping will override name configured in {@link SharedType#name()}.
124+
* </p><br>
125+
*
126+
* <p>
127+
* <b>Custom code snippet:</b><br>
128+
* Clients can provide custom code snippets to be injected into the emitted file.
129+
* This can be useful when e.g. a 3rd party type is referenced at client code.
130+
* By default, SharedType will search files "sharedtype-custom-code.ts", "sharedtype-custom-code.rs" respectively on cmd path.
131+
* File paths can be configured via global properties.
124132
* </p>
125133
*
126134
* <br>

client-test/rust/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
mod types;
22

3+
pub static CUSTOM_CODE_TYPE: types::CustomInjectedStruct = types::CustomInjectedStruct {
4+
field: 33,
5+
};
6+
37
#[cfg(test)]
48
mod tests {
59
use std::collections::HashMap;

client-test/typescript/package-lock.json

Lines changed: 158 additions & 158 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client-test/typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"description": "typescript-client-test",
1313
"devDependencies": {
1414
"typescript": "5.6.3",
15-
"vitest": "3.0.9"
15+
"vitest": "3.1.1"
1616
}
1717
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { JavaClass } from "./index.java8.js";
1+
import type { CustomCode, JavaClass } from "./index.java8.js";
22

33
export const javaClass: JavaClass = {
44
string: "",
@@ -7,3 +7,6 @@ export const javaClass: JavaClass = {
77
a: 0,
88
value: 0
99
};
10+
11+
export const customCodeType: CustomCode = {
12+
}

doc/Usage.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ You can customize the path by config `maven-compiler-plugin`:
7979
</compilerArgs>
8080
```
8181

82+
Properties can also be passed in as system properties, which will override the properties files, e.g.
83+
```bash
84+
./mvnw clean compile -Dsharedtype.typescript.custom-code-path=it/custom-code.ts
85+
```
86+
or
87+
```bash
88+
MAVEN_OPTS="-Dsharedtype.typescript.custom-code-path=it/custom-code.ts" ./mvnw clean compile
89+
```
90+
or can use [properties-maven-plugin](https://www.mojohaus.org/properties-maven-plugin/usage.html#set-system-properties) to set system properties for the build.
91+
8292
See [Default Properties](../processor/src/main/resources/sharedtype-default.properties) for details.
8393

8494
#### Per annotation options

it/custom-code.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// test code snippet
2+
pub struct CustomInjectedStruct {
3+
pub field: i32,
4+
}

it/custom-code.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// test code snippet
2+
export interface CustomCode {
3+
}

it/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@
106106
<includes>${compileClasses}</includes>
107107
</configuration>
108108
</plugin>
109+
<plugin>
110+
<groupId>org.codehaus.mojo</groupId>
111+
<artifactId>properties-maven-plugin</artifactId>
112+
<version>1.2.1</version>
113+
<executions>
114+
<execution>
115+
<goals>
116+
<goal>set-system-properties</goal>
117+
</goals>
118+
<configuration>
119+
<properties>
120+
<property>
121+
<name>sharedtype.typescript.custom-code-path</name>
122+
<value>it/custom-code.ts</value>
123+
</property>
124+
<property>
125+
<name>sharedtype.rust.custom-code-path</name>
126+
<value>it/custom-code.rs</value>
127+
</property>
128+
</properties>
129+
</configuration>
130+
</execution>
131+
</executions>
132+
</plugin>
109133
</plugins>
110134
</build>
111135
<profiles>

processor/src/main/java/online/sharedtype/processor/context/Props.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static final class Typescript {
4141
private final EnumFormat enumFormat;
4242
private final FieldReadonlyType fieldReadonlyType;
4343
private final Map<String, String> typeMappings;
44+
private final String customCodePath;
4445

4546
@Getter
4647
public enum OptionalFieldFormat {
@@ -88,5 +89,6 @@ public static final class Rust {
8889
private final Set<String> defaultTypeMacros;
8990
private final String targetDatetimeTypeLiteral;
9091
private final Map<String, String> typeMappings;
92+
private final String customCodePath;
9193
}
9294
}

0 commit comments

Comments
 (0)