|
26 | 26 |
|
27 | 27 |
|
28 | 28 |
|
29 | | -let replace s env : string = |
30 | | - Bsb_regex.global_substitute "\\${bsb:\\([-a-zA-Z0-9]+\\)}" |
31 | | - (fun (_s : string) templates -> |
32 | | - match templates with |
33 | | - | key::_ -> |
| 29 | +let replace s env : string = |
| 30 | + Bsb_regex.global_substitute "\\${bsb:\\([-a-zA-Z0-9]+\\)}" |
| 31 | + (fun (_s : string) templates -> |
| 32 | + match templates with |
| 33 | + | key::_ -> |
34 | 34 | String_hashtbl.find_exn env key |
35 | | - | _ -> assert false |
| 35 | + | _ -> assert false |
36 | 36 | ) s |
37 | 37 |
|
38 | | -let (//) = Filename.concat |
| 38 | +let (//) = Filename.concat |
39 | 39 |
|
40 | 40 |
|
41 | | -let run_npm_link cwd name = |
42 | | - Format.fprintf Format.std_formatter |
43 | | - "Symlink bs-platform in %s @." (cwd//name); |
44 | | - if Ext_sys.is_windows_or_cygwin then |
45 | | - begin |
46 | | - let npm_link = "npm link bs-platform" in |
47 | | - let exit_code = Sys.command npm_link in |
48 | | - if exit_code <> 0 then |
| 41 | +let run_npm_link cwd name = |
| 42 | + Format.fprintf Format.std_formatter |
| 43 | + "Symlink bs-platform in %s @." (cwd//name); |
| 44 | + if Ext_sys.is_windows_or_cygwin then |
| 45 | + begin |
| 46 | + let npm_link = "npm link bs-platform" in |
| 47 | + let exit_code = Sys.command npm_link in |
| 48 | + if exit_code <> 0 then |
49 | 49 | begin |
50 | 50 | prerr_endline ("failed to run : " ^ npm_link); |
51 | 51 | exit exit_code |
52 | 52 | end |
53 | | - end |
54 | | - else |
55 | | - begin |
56 | | - let (//) = Filename.concat in |
| 53 | + end |
| 54 | + else |
| 55 | + begin |
| 56 | + let (//) = Filename.concat in |
57 | 57 | let node_bin = "node_modules" // ".bin" in |
58 | 58 | Bsb_build_util.mkp node_bin; |
59 | | - let p = ".." // "bs-platform" // "lib" in |
60 | | - let link a = |
61 | | - Unix.symlink (p//a) (node_bin // a) in |
62 | | - link "bsb" ; |
| 59 | + let p = ".." // "bs-platform" // "lib" in |
| 60 | + let link a = |
| 61 | + Unix.symlink (p//a) (node_bin // a) in |
| 62 | + link "bsb" ; |
63 | 63 | link "bsc" ; |
64 | | - link "bsrefmt"; |
| 64 | + link "bsrefmt"; |
65 | 65 | Unix.symlink |
66 | 66 | (Filename.dirname (Filename.dirname Sys.executable_name)) |
67 | 67 | (Filename.concat "node_modules" "bs-platform") |
68 | 68 | end |
69 | | -let enter_dir cwd x action = |
70 | | - Unix.chdir x ; |
71 | | - match action () with |
72 | | - | exception e -> Unix.chdir cwd ; raise e |
73 | | - | v -> v |
| 69 | +let enter_dir cwd x action = |
| 70 | + Unix.chdir x ; |
| 71 | + match action () with |
| 72 | + | exception e -> Unix.chdir cwd ; raise e |
| 73 | + | v -> v |
74 | 74 |
|
75 | 75 |
|
76 | | -let rec process_theme_aux env cwd (x : OCamlRes.Res.node) = |
77 | | - match x with |
78 | | - | File (name,content) -> |
| 76 | +let rec process_theme_aux env cwd (x : OCamlRes.Res.node) = |
| 77 | + match x with |
| 78 | + | File (name,content) -> |
79 | 79 | Ext_io.write_file (cwd // name) (replace content env) |
80 | | - | Dir (current, nodes) -> |
| 80 | + | Dir (current, nodes) -> |
81 | 81 | Unix.mkdir (cwd//current) 0o777; |
82 | 82 | List.iter (fun x -> process_theme_aux env (cwd//current) x ) nodes |
83 | 83 |
|
84 | 84 | let list_themes () = |
85 | 85 | Format.fprintf Format.std_formatter "Available themes: @."; |
86 | | - Bsb_templates.root |
| 86 | + Bsb_templates.root |
87 | 87 | |> |
88 | 88 | List.iter (fun (x : OCamlRes.Res.node) -> |
89 | | - match x with |
90 | | - | Dir (x, _) -> |
91 | | - Format.fprintf Format.std_formatter "%s@." x |
| 89 | + match x with |
| 90 | + | Dir (x, _) -> |
| 91 | + Format.fprintf Format.std_formatter "%s@." x |
92 | 92 |
|
93 | 93 | | _ -> () |
94 | | - ) |
| 94 | + ) |
95 | 95 |
|
96 | | -(* @raise [Not_found] *) |
97 | | -let process_themes env theme proj_dir (themes : OCamlRes.Res.node list ) = |
98 | | - match List.find (fun (x : OCamlRes.Res.node) -> |
99 | | - match x with |
| 96 | +(* @raise [Not_found] *) |
| 97 | +let process_themes env theme proj_dir (themes : OCamlRes.Res.node list ) = |
| 98 | + match List.find (fun (x : OCamlRes.Res.node) -> |
| 99 | + match x with |
100 | 100 | | Dir (dir, _) -> dir = theme |
101 | | - | File _ -> false |
102 | | - ) themes with |
103 | | - | exception Not_found -> |
| 101 | + | File _ -> false |
| 102 | + ) themes with |
| 103 | + | exception Not_found -> |
104 | 104 | list_themes (); |
105 | 105 | raise (Arg.Bad( "theme " ^ theme ^ " not found") ) |
106 | | - | Dir(_theme, nodes ) -> |
| 106 | + | Dir(_theme, nodes ) -> |
107 | 107 | List.iter (fun node -> process_theme_aux env proj_dir node ) nodes |
108 | | - | _ -> assert false |
| 108 | + | _ -> assert false |
109 | 109 |
|
110 | 110 | (** TODO: run npm link *) |
111 | | -let init_sample_project ~cwd ~theme name = |
112 | | - let env = String_hashtbl.create 0 in |
113 | | - List.iter (fun (k,v) -> String_hashtbl.add env k v ) [ |
| 111 | +let init_sample_project ~cwd ~theme name = |
| 112 | + let env = String_hashtbl.create 0 in |
| 113 | + List.iter (fun (k,v) -> String_hashtbl.add env k v ) [ |
114 | 114 | "proj-version", "0.1.0"; |
115 | 115 | "bs-version", Bs_version.version; |
116 | 116 | "bsb" , Filename.current_dir_name // "node_modules" // ".bin" // "bsb" |
117 | 117 | ]; |
118 | | - let action = fun _ -> |
119 | | - process_themes env theme Filename.current_dir_name Bsb_templates.root; |
| 118 | + let action = fun _ -> |
| 119 | + process_themes env theme Filename.current_dir_name Bsb_templates.root; |
120 | 120 | run_npm_link cwd name |
121 | | - in |
122 | | - begin match name with |
123 | | - | "." -> |
| 121 | + in |
| 122 | + begin match name with |
| 123 | + | "." -> |
124 | 124 | let name = Filename.basename cwd in |
125 | | - if Ext_namespace.is_valid_npm_package_name name then |
126 | | - begin |
| 125 | + if Ext_namespace.is_valid_npm_package_name name then |
| 126 | + begin |
127 | 127 | String_hashtbl.add env "name" name; |
128 | 128 | action () |
129 | 129 | end |
130 | | - else |
| 130 | + else |
131 | 131 | begin |
132 | | - Format.fprintf Format.err_formatter |
| 132 | + Format.fprintf Format.err_formatter |
133 | 133 | "@{<error>Invalid package name@} %S.@} The project name must be a valid npm name, thus can't contain upper-case letters, for example." |
134 | 134 | name ; |
135 | | - exit 2 |
| 135 | + exit 2 |
136 | 136 | end |
137 | 137 |
|
138 | | - | _ -> |
139 | | - if Ext_namespace.is_valid_npm_package_name name |
140 | | - then begin |
141 | | - Format.fprintf Format.std_formatter "Making directory %s@." name; |
142 | | - if Sys.file_exists name then |
143 | | - begin |
144 | | - Format.fprintf Format.err_formatter "%s already existed@." name ; |
| 138 | + | _ -> |
| 139 | + if Ext_namespace.is_valid_npm_package_name name |
| 140 | + then begin |
| 141 | + Format.fprintf Format.std_formatter "Making directory %s@." name; |
| 142 | + if Sys.file_exists name then |
| 143 | + begin |
| 144 | + Format.fprintf Format.err_formatter "@{<error>%s already exists@}@." name ; |
145 | 145 | exit 2 |
146 | | - end |
| 146 | + end |
147 | 147 | else |
148 | | - begin |
149 | | - Unix.mkdir name 0o777; |
| 148 | + begin |
| 149 | + Unix.mkdir name 0o777; |
150 | 150 | String_hashtbl.add env "name" name; |
151 | 151 | enter_dir cwd name action |
152 | 152 | end |
153 | | - end else begin |
154 | | - Format.fprintf Format.err_formatter |
| 153 | + end else begin |
| 154 | + Format.fprintf Format.err_formatter |
155 | 155 | "@{<error>Invalid package name@} %S.@} The project name must be a valid npm name, thus can't contain upper-case letters, for example." |
156 | 156 | name ; |
157 | | - exit 2 |
158 | | - end |
| 157 | + exit 2 |
| 158 | + end |
159 | 159 | end |
160 | 160 |
|
161 | 161 |
|
|
0 commit comments