##// END OF EJS Templates
working on di decorators
cin -
r109:f71c50acc9f9 ioc ts support
parent child
Show More
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -1,234 +1,236
1 plugins {
1 plugins {
2 id "org.implab.gradle-typescript" version "1.3.3"
2 id "org.implab.gradle-typescript" version "1.3.3"
3 id "org.implab.gradle-hg"
3 id "org.implab.gradle-hg"
4 id "ivy-publish"
4 id "ivy-publish"
5 }
5 }
6
6
7 if (!symbols in ['local', 'pack', 'none'])
7 if (!symbols in ['local', 'pack', 'none'])
8 throw new Exception("The symbols property value is invalid: $symbols");
8 throw new Exception("The symbols property value is invalid: $symbols");
9
9
10 if (!flavour in ['browser', 'node'])
10 if (!flavour in ['browser', 'node'])
11 throw new Exception("The flavour property value is invalid: $flavour");
11 throw new Exception("The flavour property value is invalid: $flavour");
12
12
13 ext {
13 ext {
14 packageName = flavour == 'browser' ? "@$npmScope/$name-amd" : "@$npmScope/$name"
14 packageName = flavour == 'browser' ? "@$npmScope/$name-amd" : "@$npmScope/$name"
15 lint = project.hasProperty('lint') ? project.lint ?: true : false
15 lint = project.hasProperty('lint') ? project.lint ?: true : false
16 }
16 }
17
17
18 sources {
18 sources {
19 amd {
19 amd {
20 typings {
20 typings {
21 srcDir main.output.typingsDir
21 srcDir main.output.typingsDir
22 }
22 }
23 }
23 }
24
24
25 cjs {
25 cjs {
26 typings {
26 typings {
27 srcDir main.output.typingsDir
27 srcDir main.output.typingsDir
28 }
28 }
29 }
29 }
30
30
31 testAmd {
31 testAmd {
32 typings {
32 typings {
33 srcDir main.output.typingsDir
33 srcDir main.output.typingsDir
34 srcDir amd.output.typingsDir
34 srcDir amd.output.typingsDir
35 srcDir test.output.typingsDir
35 srcDir test.output.typingsDir
36 }
36 }
37 }
37 }
38
38
39 testCjs {
39 testCjs {
40 typings {
40 typings {
41 srcDir main.output.typingsDir
41 srcDir main.output.typingsDir
42 srcDir cjs.output.typingsDir
42 srcDir cjs.output.typingsDir
43 srcDir test.output.typingsDir
43 srcDir test.output.typingsDir
44 }
44 }
45 }
45 }
46 }
46 }
47
47
48 typescript {
48 typescript {
49 compilerOptions {
49 compilerOptions {
50 types = []
50 types = []
51 declaration = true
51 declaration = true
52 experimentalDecorators = true
53 //strict = true
52
54
53 if(symbols != 'none') {
55 if(symbols != 'none') {
54 sourceMap = true
56 sourceMap = true
55 sourceRoot = packageName
57 sourceRoot = packageName
56 }
58 }
57
59
58 if (flavour == 'node') {
60 if (flavour == 'node') {
59 module = "commonjs"
61 module = "commonjs"
60 target = "es2017"
62 target = "es2017"
61 lib = ["es2017", "dom", "scripthost"]
63 lib = ["es2017", "dom", "scripthost"]
62 } else if (flavour == 'browser') {
64 } else if (flavour == 'browser') {
63 module = "amd"
65 module = "amd"
64 target = "es5"
66 target = "es5"
65 lib = ["es5", "dom", "scripthost", "es2015.promise", "es2015.symbol", "es2015.iterable" ]
67 lib = ["es5", "dom", "scripthost", "es2015.promise", "es2015.symbol", "es2015.iterable" ]
66 }
68 }
67 }
69 }
68 tscCmd = "$projectDir/node_modules/.bin/tsc"
70 tscCmd = "$projectDir/node_modules/.bin/tsc"
69 tsLintCmd = "$projectDir/node_modules/.bin/tslint"
71 tsLintCmd = "$projectDir/node_modules/.bin/tslint"
70 esLintCmd = "$projectDir/node_modules/.bin/eslint"
72 esLintCmd = "$projectDir/node_modules/.bin/eslint"
71 }
73 }
72
74
73 tasks.matching{ it.name =~ /^lint/ }.configureEach {
75 tasks.matching{ it.name =~ /^lint/ }.configureEach {
74 onlyIf { lint }
76 onlyIf { lint }
75 }
77 }
76
78
77 if (symbols == 'local') {
79 if (symbols == 'local') {
78 tasks.matching{ it.name =~ /^configureTs/ }.configureEach {
80 tasks.matching{ it.name =~ /^configureTs/ }.configureEach {
79 compilerOptions {
81 compilerOptions {
80 sourceRoot = "file://" + it.rootDir
82 sourceRoot = "file://" + it.rootDir
81 }
83 }
82 }
84 }
83 }
85 }
84
86
85 task printVersion {
87 task printVersion {
86 doLast {
88 doLast {
87 println "packageName: $packageName";
89 println "packageName: $packageName";
88 println "version: $version";
90 println "version: $version";
89 println "flavour: $flavour";
91 println "flavour: $flavour";
90 println "target: $typescript.compilerOptions.target";
92 println "target: $typescript.compilerOptions.target";
91 println "module: $typescript.compilerOptions.module";
93 println "module: $typescript.compilerOptions.module";
92 println "lint: $lint";
94 println "lint: $lint";
93 println "symbols: $symbols";
95 println "symbols: $symbols";
94 }
96 }
95 }
97 }
96
98
97 npmPackMeta {
99 npmPackMeta {
98 meta {
100 meta {
99 name = packageName
101 name = packageName
100 }
102 }
101 }
103 }
102
104
103 configureTsCjs {
105 configureTsCjs {
104 dependsOn sources.main.output
106 dependsOn sources.main.output
105 compilerOptions {
107 compilerOptions {
106 types += [ "node" ]
108 types += [ "node" ]
107 }
109 }
108 }
110 }
109
111
110 configureTsAmd {
112 configureTsAmd {
111 dependsOn sources.main.output
113 dependsOn sources.main.output
112 compilerOptions {
114 compilerOptions {
113 types += [ "requirejs", "dojo-typings" ]
115 types += [ "requirejs", "dojo-typings" ]
114 }
116 }
115 }
117 }
116
118
117 test {
119 test {
118 workingDir layout.buildDirectory.dir("test");
120 workingDir layout.buildDirectory.dir("test");
119 commandLine "node", "tests/index.js"
121 commandLine "node", "tests/index.js"
120 }
122 }
121
123
122 assemble {
124 assemble {
123 if (flavour == 'browser') {
125 if (flavour == 'browser') {
124 dependsOn sources.amd.output
126 dependsOn sources.amd.output
125 from sources.amd.output.compiledDir
127 from sources.amd.output.compiledDir
126 from sources.amd.resources
128 from sources.amd.resources
127 }
129 }
128 if (flavour == 'node') {
130 if (flavour == 'node') {
129 dependsOn sources.cjs.output
131 dependsOn sources.cjs.output
130 from sources.cjs.output.compiledDir
132 from sources.cjs.output.compiledDir
131 from sources.cjs.resources
133 from sources.cjs.resources
132 }
134 }
133 }
135 }
134
136
135 assembleTest {
137 assembleTest {
136 if (flavour == 'browser') {
138 if (flavour == 'browser') {
137 dependsOn sources.amd.output, sources.testAmd.output
139 dependsOn sources.amd.output, sources.testAmd.output
138
140
139 from sources.amd.output.compiledDir
141 from sources.amd.output.compiledDir
140 from sources.testAmd.output.compiledDir
142 from sources.testAmd.output.compiledDir
141 from sources.amd.resources
143 from sources.amd.resources
142 from sources.testAmd.resources
144 from sources.testAmd.resources
143 }
145 }
144 if (flavour == 'node') {
146 if (flavour == 'node') {
145 dependsOn sources.cjs.output, sources.testCjs.output
147 dependsOn sources.cjs.output, sources.testCjs.output
146
148
147 from sources.cjs.output.compiledDir
149 from sources.cjs.output.compiledDir
148 from sources.testCjs.output.compiledDir
150 from sources.testCjs.output.compiledDir
149 from sources.cjs.resources
151 from sources.cjs.resources
150 from sources.testCjs.resources
152 from sources.testCjs.resources
151 }
153 }
152 }
154 }
153
155
154 typings {
156 typings {
155 if (flavour == 'browser') {
157 if (flavour == 'browser') {
156 dependsOn sources.amd.output
158 dependsOn sources.amd.output
157 from sources.amd.output.typingsDir
159 from sources.amd.output.typingsDir
158 }
160 }
159 if (flavour == 'node') {
161 if (flavour == 'node') {
160 dependsOn sources.cjs.output
162 dependsOn sources.cjs.output
161 from sources.cjs.output.typingsDir
163 from sources.cjs.output.typingsDir
162 }
164 }
163 }
165 }
164
166
165 task npmPackTypings(type: Copy) {
167 task npmPackTypings(type: Copy) {
166 npmPackContents.dependsOn it
168 npmPackContents.dependsOn it
167 dependsOn sources.main.output
169 dependsOn sources.main.output
168
170
169 from sources.main.output.typingsDir
171 from sources.main.output.typingsDir
170
172
171 if (flavour == 'browser') {
173 if (flavour == 'browser') {
172 dependsOn sources.amd.output
174 dependsOn sources.amd.output
173 from sources.amd.output.typingsDir
175 from sources.amd.output.typingsDir
174 }
176 }
175 if (flavour == 'node') {
177 if (flavour == 'node') {
176 dependsOn sources.cjs.output
178 dependsOn sources.cjs.output
177 from sources.cjs.output.typingsDir
179 from sources.cjs.output.typingsDir
178 }
180 }
179
181
180 into npm.packageDir
182 into npm.packageDir
181 }
183 }
182
184
183 task npmPackSources(type: Copy) {
185 task npmPackSources(type: Copy) {
184 from sources.main.ts
186 from sources.main.ts
185 if (symbols == 'pack') {
187 if (symbols == 'pack') {
186 npmPackContents.dependsOn npmPackSources
188 npmPackContents.dependsOn npmPackSources
187 }
189 }
188
190
189 if (flavour == 'browser') {
191 if (flavour == 'browser') {
190 from sources.amd.ts
192 from sources.amd.ts
191 }
193 }
192 if (flavour == 'node') {
194 if (flavour == 'node') {
193 from sources.cjs.ts
195 from sources.cjs.ts
194 }
196 }
195
197
196 into npm.packageDir.dir("_src")
198 into npm.packageDir.dir("_src")
197 }
199 }
198
200
199
201
200
202
201 task packJsTar(type: Tar) {
203 task packJsTar(type: Tar) {
202 dependsOn assemble;
204 dependsOn assemble;
203
205
204 archiveBaseName = provider { packageName }
206 archiveBaseName = provider { packageName }
205
207
206 destinationDirectory = buildDir
208 destinationDirectory = buildDir
207 archiveClassifier = provider { typescript.compilerOptions.module }
209 archiveClassifier = provider { typescript.compilerOptions.module }
208 compression = Compression.GZIP
210 compression = Compression.GZIP
209
211
210 from(assemble.outputs)
212 from(assemble.outputs)
211
213
212 doLast {
214 doLast {
213 println archiveName;
215 println archiveName;
214 }
216 }
215 }
217 }
216
218
217 task packTypingsTar(type: Tar) {
219 task packTypingsTar(type: Tar) {
218 }
220 }
219
221
220 publishing {
222 publishing {
221 publications {
223 publications {
222 local(IvyPublication) {
224 local(IvyPublication) {
223 artifact(packJsTar) {
225 artifact(packJsTar) {
224 type = "js"
226 type = "js"
225 }
227 }
226 }
228 }
227 }
229 }
228
230
229 repositories {
231 repositories {
230 ivy {
232 ivy {
231 url "ivy-repo"
233 url "ivy-repo"
232 }
234 }
233 }
235 }
234 } No newline at end of file
236 }
@@ -1,37 +1,50
1 import { Constructor } from "../interfaces";
1 import { Constructor } from "../interfaces";
2
2
3 export interface InjectOptions {
3 export interface InjectOptions {
4 lazy?: boolean;
4 lazy?: boolean;
5 }
5 }
6
6
7 type Setter<T = any> = (v: T) => void;
7 type Setter<T = any> = (v: T) => void;
8
8
9 type Injector<T> = {
9 type Injector<T> = {
10 [k in keyof T]: Setter;
10 [k in keyof T]: Setter;
11 };
11 };
12
12
13 type Compatible<T1, T2> = T1 extends T2 ? any : never;
14
15 type SetterType<T> = T extends (v: infer V) => void ? V : never;
16
17 type Tuple<T = any> = Parameters<(...args: T[]) => void>;
18
19 interface Newable<A extends Tuple, T> {
20 new (...params: A): T;
21 prototype: T;
22 }
23
24 type MapTuple<T, A extends (keyof T)[]> = { [K in keyof A] : K extends number ? T[ A[K] ] : A[K] };
25
13 export class Builder<T, S> {
26 export class Builder<T, S> {
14 provides(name: string) {
27 provides() {
15 return <C extends Constructor<T>>(constructor: C) => {
28 return <C extends Constructor<T>>(constructor: C) => {
16 return constructor;
29 return constructor;
17 };
30 };
18 }
31 }
19
32
20 inject<K extends keyof S>(dependency: K) {
33 inject<K extends keyof S>(dependency: K) {
21 return <M extends keyof T>(target: any, memberName: M, descriptor: TypedPropertyDescriptor<Setter<S[K]>> ) => {
34 // K = "bar"
35 // M = "setValue"
36 // S[K] = Bar
37 // T[M] = (value: string) => void
38 // P[m] = (value: V) => void
39 return <P, M extends keyof (T | P)>(target: P, memberName: M, descriptor: TypedPropertyDescriptor<Compatible<T[M], Setter<S[K]>>>) => {
22
40
23 };
41 };
24 }
42 }
25
43
26 prop<K extends keyof S>(dependency: K) {
44 dependencies<D extends (keyof S)[]>(...deps: D) {
27 return <M extends keyof T>(target: any, memberName: M, descriptor: TypedPropertyDescriptor<S[K]> ) => {
45 return <C extends Constructor<T>>(constructor: MapTuple<S, D> extends ConstructorParameters<C> ? C : never) => {
28
46 return constructor;
29 };
47 } ;
30 }
48 }
49
31 }
50 }
32
33 export function inject<I extends Injector<I> >(dependency: string) {
34 return <M extends keyof I>(target: any, memberName: M, descriptor: TypedPropertyDescriptor<I[M]> ) => {
35
36 };
37 }
@@ -1,12 +1,18
1 import { Foo } from "./Foo";
1 import { Foo } from "./Foo";
2
2
3 export class Bar {
3 export class Bar {
4 name = "bar";
4 name = "bar";
5
5
6 foo: Foo;
6 foo: Foo | undefined;
7
7
8 constructor(_opts) {
8 constructor(_opts?: { foo?: Foo; }) {
9 if (_opts && _opts.foo)
9 if (_opts && _opts.foo)
10 this.foo = _opts.foo;
10 this.foo = _opts.foo;
11 }
11 }
12
13 getFoo() {
14 if (this.foo === undefined)
15 throw new Error("The foo isn't set");
16 return this.foo;
12 }
17 }
18 }
@@ -1,23 +1,33
1 import { Builder } from "../di/Annotations";
1 import { Builder } from "../di/Annotations";
2 import { Bar } from "./Bar";
2 import { Bar } from "./Bar";
3 import { Foo } from "./Foo";
3 import { Foo } from "./Foo";
4
4
5 export interface Injector<T> {
5 const builder = new Builder<Box<Bar>, { bar: Bar; foo: Foo; obj: object }>();
6 setValue(value: T);
7 }
8
6
9 const builder = new Builder<Box<any>, { bar: Bar; foo: Foo}>();
7 @builder.provides()
8 @builder.dependencies("bar")
9 export class Box<T> {
10 private _value: T | undefined;
10
11
11 @builder.provides("barBox")
12 constructor(value: T) {
12 export class Box<T> implements Injector<T> {
13 this._value = value;
13 private _value: T;
14 }
14
15
15 @builder.inject("bar")
16 @builder.inject("bar")
16 setValue(value: T) {
17 setValue(value: T) {
17 this._value = value;
18 this._value = value;
18 }
19 }
19
20
21
22 @builder.inject("foo")
23 setObj(value: object) {
24
25 }
26
20 getValue() {
27 getValue() {
28 if (this._value === undefined)
29 throw new Error("Trying to get a value from the empty box");
30
21 return this._value;
31 return this._value;
22 }
32 }
23 }
33 } No newline at end of file
@@ -1,14 +1,14
1 {
1 {
2 "extends": "../tsconfig",
2 "extends": "../tsconfig",
3 "compilerOptions": {
3 "compilerOptions": {
4 "rootDir": "ts",
4 //"rootDir": "ts",
5 "baseUrl": ".",
5 "baseUrl": ".",
6 "rootDirs": [
6 "rootDirs": [
7 "ts",
7 "ts",
8 "../main/ts"
8 "../main/ts"
9 ]
9 ]
10 },
10 },
11 "include" : [
11 "include" : [
12 "ts/**/*.ts"
12 "ts/**/*.ts"
13 ]
13 ]
14 } No newline at end of file
14 }
@@ -1,11 +1,12
1 {
1 {
2 "compilerOptions": {
2 "compilerOptions": {
3 "moduleResolution": "node",
3 "moduleResolution": "node",
4 "experimentalDecorators": true,
4 "experimentalDecorators": true,
5 "noEmitOnError": true,
5 "noEmitOnError": true,
6 "listFiles": true,
6 "listFiles": true,
7 "strict": true,
7 "types": [],
8 "types": [],
8 "target": "ES5",
9 "target": "ES5",
9 "lib": ["es5", "es2015.promise", "es2015.symbol", "es2015.iterable", "dom", "scripthost"]
10 "lib": ["es5", "es2015.promise", "es2015.symbol", "es2015.iterable", "dom", "scripthost"]
10 }
11 }
11 } No newline at end of file
12 }
General Comments 0
You need to be logged in to leave comments. Login now