@@ -26,6 +26,8 @@ export class BobProject {
26
26
return list ;
27
27
}
28
28
29
+ public
30
+
29
31
public createRules ( ) : OutFiles {
30
32
let output : OutFiles = { } ;
31
33
const subdirs = Object . keys ( this . dirTargets ) ;
@@ -34,15 +36,66 @@ export class BobProject {
34
36
35
37
for ( const subdir in this . dirTargets ) {
36
38
const targets = this . dirTargets [ subdir ] ;
37
- let lines : string [ ] = [ ] ;
39
+ const currentRulesFile = path . join ( subdir , `Rules.mk` ) ;
40
+ const currentFullPath = path . join ( this . targets . getCwd ( ) , currentRulesFile ) ;
41
+ let rulesContent : string [ ] = [ ] ;
42
+
43
+ if ( existsSync ( currentFullPath ) ) {
44
+ rulesContent = readFileSync ( currentFullPath , { encoding : `utf-8` } ) . split ( `\n` ) ;
45
+ }
46
+
47
+ const rulesFile = new RulesFile ( subdir , rulesContent ) ;
38
48
39
49
for ( let target of targets ) {
40
- lines . push ( ` ${ target . systemName } . ${ target . type } : ${ path . relative ( subdir , target . relativePath ) } ${ target . deps . filter ( d => d . reference !== true ) . map ( d => ` ${ d . systemName } . ${ d . type } ` ) . join ( ` ` ) } ` ) ;
50
+ rulesFile . applyRule ( target ) ;
41
51
}
42
52
43
- output [ path . join ( subdir , `Rules.mk` ) ] = lines . join ( `\n` ) ;
53
+ output [ currentRulesFile ] = rulesFile . getContent ( ) ;
44
54
}
45
55
46
56
return output ;
47
57
}
58
+ }
59
+
60
+ interface Rule {
61
+ ogLine : string ;
62
+ target ?: String ,
63
+ content ?: String ,
64
+ isUserWritten ?: boolean ,
65
+ } ;
66
+
67
+ class RulesFile {
68
+ private parsed : Rule [ ] = [ ] ;
69
+ constructor ( private subdir : string , lines : string [ ] ) {
70
+ for ( let line of lines ) {
71
+ let currentRule : Rule = { ogLine : line } ;
72
+ if ( line . includes ( `:` ) ) {
73
+ const [ target , content ] = line . split ( `:` ) ;
74
+ currentRule . target = target . trim ( ) . toUpperCase ( ) ;
75
+ currentRule . content = content . trim ( ) ;
76
+ currentRule . isUserWritten = content . includes ( `=` ) || content . trimStart ( ) . startsWith ( `#` ) ;
77
+ }
78
+
79
+ this . parsed . push ( currentRule ) ;
80
+ }
81
+ }
82
+
83
+ applyRule ( target : ILEObjectTarget ) {
84
+ const objName = `${ target . systemName } .${ target . type } ` ;
85
+
86
+ const existingLine = this . parsed . find ( r => r . target === objName && r . isUserWritten !== true ) ;
87
+
88
+ const lineContent = `${ path . relative ( this . subdir , target . relativePath ) } ${ target . deps . filter ( d => d . reference !== true ) . map ( d => `${ d . systemName } .${ d . type } ` ) . join ( ` ` ) } ` . trimEnd ( ) ;
89
+
90
+ if ( existingLine ) {
91
+ existingLine . ogLine = `${ objName } : ${ lineContent } ` ;
92
+ existingLine . content = lineContent ;
93
+ } else {
94
+ this . parsed . push ( { ogLine : `${ objName } : ${ lineContent } ` , target : objName , content : lineContent } ) ;
95
+ }
96
+ }
97
+
98
+ getContent ( ) {
99
+ return this . parsed . map ( r => r . ogLine ) . join ( `\n` ) ;
100
+ }
48
101
}
0 commit comments