diff --git a/jscomp/bsb/bsb_theme_init.ml b/jscomp/bsb/bsb_theme_init.ml index fb9359c21d..ed8c5d9b58 100644 --- a/jscomp/bsb/bsb_theme_init.ml +++ b/jscomp/bsb/bsb_theme_init.ml @@ -26,136 +26,136 @@ -let replace s env : string = - Bsb_regex.global_substitute "\\${bsb:\\([-a-zA-Z0-9]+\\)}" - (fun (_s : string) templates -> - match templates with - | key::_ -> +let replace s env : string = + Bsb_regex.global_substitute "\\${bsb:\\([-a-zA-Z0-9]+\\)}" + (fun (_s : string) templates -> + match templates with + | key::_ -> String_hashtbl.find_exn env key - | _ -> assert false + | _ -> assert false ) s -let (//) = Filename.concat +let (//) = Filename.concat -let run_npm_link cwd name = - Format.fprintf Format.std_formatter - "Symlink bs-platform in %s @." (cwd//name); - if Ext_sys.is_windows_or_cygwin then - begin - let npm_link = "npm link bs-platform" in - let exit_code = Sys.command npm_link in - if exit_code <> 0 then +let run_npm_link cwd name = + Format.fprintf Format.std_formatter + "Symlink bs-platform in %s @." (cwd//name); + if Ext_sys.is_windows_or_cygwin then + begin + let npm_link = "npm link bs-platform" in + let exit_code = Sys.command npm_link in + if exit_code <> 0 then begin prerr_endline ("failed to run : " ^ npm_link); exit exit_code end - end - else - begin - let (//) = Filename.concat in + end + else + begin + let (//) = Filename.concat in let node_bin = "node_modules" // ".bin" in Bsb_build_util.mkp node_bin; - let p = ".." // "bs-platform" // "lib" in - let link a = - Unix.symlink (p//a) (node_bin // a) in - link "bsb" ; + let p = ".." // "bs-platform" // "lib" in + let link a = + Unix.symlink (p//a) (node_bin // a) in + link "bsb" ; link "bsc" ; - link "bsrefmt"; + link "bsrefmt"; Unix.symlink (Filename.dirname (Filename.dirname Sys.executable_name)) (Filename.concat "node_modules" "bs-platform") end -let enter_dir cwd x action = - Unix.chdir x ; - match action () with - | exception e -> Unix.chdir cwd ; raise e - | v -> v +let enter_dir cwd x action = + Unix.chdir x ; + match action () with + | exception e -> Unix.chdir cwd ; raise e + | v -> v -let rec process_theme_aux env cwd (x : OCamlRes.Res.node) = - match x with - | File (name,content) -> +let rec process_theme_aux env cwd (x : OCamlRes.Res.node) = + match x with + | File (name,content) -> Ext_io.write_file (cwd // name) (replace content env) - | Dir (current, nodes) -> + | Dir (current, nodes) -> Unix.mkdir (cwd//current) 0o777; List.iter (fun x -> process_theme_aux env (cwd//current) x ) nodes let list_themes () = Format.fprintf Format.std_formatter "Available themes: @."; - Bsb_templates.root + Bsb_templates.root |> List.iter (fun (x : OCamlRes.Res.node) -> - match x with - | Dir (x, _) -> - Format.fprintf Format.std_formatter "%s@." x + match x with + | Dir (x, _) -> + Format.fprintf Format.std_formatter "%s@." x | _ -> () - ) + ) -(* @raise [Not_found] *) -let process_themes env theme proj_dir (themes : OCamlRes.Res.node list ) = - match List.find (fun (x : OCamlRes.Res.node) -> - match x with +(* @raise [Not_found] *) +let process_themes env theme proj_dir (themes : OCamlRes.Res.node list ) = + match List.find (fun (x : OCamlRes.Res.node) -> + match x with | Dir (dir, _) -> dir = theme - | File _ -> false - ) themes with - | exception Not_found -> + | File _ -> false + ) themes with + | exception Not_found -> list_themes (); raise (Arg.Bad( "theme " ^ theme ^ " not found") ) - | Dir(_theme, nodes ) -> + | Dir(_theme, nodes ) -> List.iter (fun node -> process_theme_aux env proj_dir node ) nodes - | _ -> assert false + | _ -> assert false (** TODO: run npm link *) -let init_sample_project ~cwd ~theme name = - let env = String_hashtbl.create 0 in - List.iter (fun (k,v) -> String_hashtbl.add env k v ) [ +let init_sample_project ~cwd ~theme name = + let env = String_hashtbl.create 0 in + List.iter (fun (k,v) -> String_hashtbl.add env k v ) [ "proj-version", "0.1.0"; "bs-version", Bs_version.version; "bsb" , Filename.current_dir_name // "node_modules" // ".bin" // "bsb" ]; - let action = fun _ -> - process_themes env theme Filename.current_dir_name Bsb_templates.root; + let action = fun _ -> + process_themes env theme Filename.current_dir_name Bsb_templates.root; run_npm_link cwd name - in - begin match name with - | "." -> + in + begin match name with + | "." -> let name = Filename.basename cwd in - if Ext_namespace.is_valid_npm_package_name name then - begin + if Ext_namespace.is_valid_npm_package_name name then + begin String_hashtbl.add env "name" name; action () end - else + else begin - Format.fprintf Format.err_formatter + Format.fprintf Format.err_formatter "@{Invalid package name@} %S.@} The project name must be a valid npm name, thus can't contain upper-case letters, for example." name ; - exit 2 + exit 2 end - | _ -> - if Ext_namespace.is_valid_npm_package_name name - then begin - Format.fprintf Format.std_formatter "Making directory %s@." name; - if Sys.file_exists name then - begin - Format.fprintf Format.err_formatter "%s already existed@." name ; + | _ -> + if Ext_namespace.is_valid_npm_package_name name + then begin + Format.fprintf Format.std_formatter "Making directory %s@." name; + if Sys.file_exists name then + begin + Format.fprintf Format.err_formatter "@{%s already exists@}@." name ; exit 2 - end + end else - begin - Unix.mkdir name 0o777; + begin + Unix.mkdir name 0o777; String_hashtbl.add env "name" name; enter_dir cwd name action end - end else begin - Format.fprintf Format.err_formatter + end else begin + Format.fprintf Format.err_formatter "@{Invalid package name@} %S.@} The project name must be a valid npm name, thus can't contain upper-case letters, for example." name ; - exit 2 - end + exit 2 + end end