Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 214 lines (168 sloc) 6.36 kb
ce8151b @VictorNicollet Improve installer, extend tool
authored
1 (* Ohm is © 2012 Victor Nicollet *)
2
34e307b @VictorNicollet Installing ohm-tool
authored
3 let error fmt = Printf.ksprintf (fun s -> print_string "[FAIL] " ; print_endline s ; exit 1) fmt
4
ce8151b @VictorNicollet Improve installer, extend tool
authored
5 let forward () =
6
7 let rec find path =
8 if (try Sys.is_directory (Filename.concat path ".ohm") with _ -> false)
9 then path
10 else if path = "/" then error "No project found ! Possible solution :
11 ohm init <project-directory>
12 cd <project-directory>
13 ohm ..."
14 else find (Filename.dirname path)
15 in
16
17 let root = find (Sys.getcwd ()) in
18 Sys.chdir root ;
19 let tool = List.fold_left Filename.concat root [".ohm";"Ohm";"tool";"tool.byte"] in
20
ffb88d8 @VictorNicollet Source code formatting
authored
21 match Array.to_list Sys.argv with
22 | [] -> error "Array.length Sys.argv = 0 ... what the hell ?"
23 | _ :: args ->
24 let command = String.concat " " (List.map Filename.quote (tool :: args)) in
25 exit (Sys.command command)
26
34e307b @VictorNicollet Installing ohm-tool
authored
27 let project, name =
ce8151b @VictorNicollet Improve installer, extend tool
authored
28 if Array.length Sys.argv <> 3 || Sys.argv.(1) <> "init" then forward () ;
a435793 @VictorNicollet New "init" keyword
authored
29 let path = Sys.argv.(2) in
b2303b3 @VictorNicollet Improved handling for "ohm init ."
authored
30 let cwd = Sys.getcwd () in
31 let path =
32 if path = ".." then Filename.dirname cwd else
33 if path = "." then cwd else
34 if Filename.is_relative path then Filename.concat cwd path else path
35 in
34e307b @VictorNicollet Installing ohm-tool
authored
36 let name = String.uncapitalize (Filename.basename path) in
b2303b3 @VictorNicollet Improved handling for "ohm init ."
authored
37 path, name
34e307b @VictorNicollet Installing ohm-tool
authored
38
39 let path seq = List.fold_left Filename.concat project seq
40
85f5182 @VictorNicollet Build plugins after install
authored
41 let readdir p =
42 let path = path p in
43 try Array.to_list (Sys.readdir path)
44 with exn -> error "Could not read directory %S : %s" path (Printexc.to_string exn)
45
34e307b @VictorNicollet Installing ohm-tool
authored
46 module Install = struct
47
48 let prefix = ref ""
49
50 let in_dir p f =
51 let old_path = Sys.getcwd () in
52 let old_prefix = !prefix in
53 Sys.chdir (path p) ;
54 prefix := (!prefix) ^ " " ;
55 Printf.printf "%s>> in %s :\n" old_prefix (path p) ;
56 ( try f () with _ -> () ) ;
57 Sys.chdir old_path ;
58 prefix := old_prefix
59
60 let run fmt =
61 Printf.ksprintf (fun command ->
62 print_string (!prefix) ;
63 print_endline command ;
64 let code = Sys.command command in
65 if code <> 0 then error "%S returned error code %d" command code
66 ) fmt
67
68 let mkdir p =
69 let _ =
70 List.fold_left (fun current seg ->
71 let p = current @ [seg] in
72 try
73 if Sys.is_directory (path p) then p else
74 error "Expected `%s` to be a directory !" (path p)
75 with _ ->
76 run "mkdir %s" (Filename.quote (path p)) ;
77 p
78 ) [] p
79 in ()
80
81 let clone p src =
82 let exists = try Sys.is_directory (path (p @ [".git"])) with _ -> false in
83 if exists then
84 in_dir p (fun () -> run "git pull --quiet")
85 else
86 run "git clone --quiet %s %s" (Filename.quote src) (Filename.quote (path p))
87
88 let symlink src dest =
89 if not (Sys.file_exists (path src)) then
90 run "ln -s %s %s" (Filename.quote (path dest)) (Filename.quote (path src))
91
92 let copy src dest =
93 if not (Sys.file_exists (path dest)) then (
94 run "cp %s %s" (Filename.quote (path src)) (Filename.quote (path dest))
95 )
96
97 let mkexec p =
98 run "chmod u+x %s" (Filename.quote (path p))
99
962d918 @VictorNicollet Generate project config
authored
100 let config () =
101 let path = path [ "ocaml" ; "configProject.ml" ] in
102 if not (Sys.file_exists path) then
103 try let chan = open_out path in
104 try let name = Printf.sprintf "let name = %S\nlet lname = %S\n"
105 name (String.lowercase name) in
106 output_string chan name ;
107 close_out chan
108 with _ -> close_out chan
109 with _ -> error "Could not write project config file %s" path
0e098b5 @VictorNicollet Build project post-installation
authored
110
6bea165 @VictorNicollet Only clean up if non-fresh
authored
111 let make fresh =
0e098b5 @VictorNicollet Build project post-installation
authored
112 in_dir [] (fun () ->
6bea165 @VictorNicollet Only clean up if non-fresh
authored
113 if not fresh then run "make clean" ;
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
114 run "make"
0e098b5 @VictorNicollet Build project post-installation
authored
115 )
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
116
117 let touch p =
118 run "touch %s" (Filename.quote (path p))
85f5182 @VictorNicollet Build plugins after install
authored
119
120 let make_plugin plugin =
121 let p = [ ".ohm" ; "Ohm-Plugins" ; plugin ; "tool" ] in
122 let exists = try Sys.is_directory (path p) with _ -> false in
123 if exists then
124 run "make -C %s" (Filename.quote (path p))
125
34e307b @VictorNicollet Installing ohm-tool
authored
126 end
127
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
128 (* Fresh install means there have been no directories or files
129 created so far. Just check whether the .install file has been created. *)
130
131 let fresh = not (Sys.file_exists (path [".install"]))
132
34e307b @VictorNicollet Installing ohm-tool
authored
133 (* Create the entire directory structure for the Ohm project. These operations
134 are all idempotent and respect data that was already created. *)
135
136 let () = List.iter Install.mkdir [
137 [ "" ] ;
138 [ ".ohm" ] ;
139 [ "ocaml" ; "plugins" ] ;
140 [ "_build" ] ;
141 [ "bot" ] ;
36c47d6 @VictorNicollet Move 500.htm
authored
142 [ "www" ] ;
143 [ "public" ]
34e307b @VictorNicollet Installing ohm-tool
authored
144 ]
145
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
146 let () = if fresh then List.iter Install.mkdir [
147 [ "assets" ; "common" ] ;
9c6b3dc @VictorNicollet New install files
authored
148 [ "assets" ; "errorPage" ];
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
149 ]
150
34e307b @VictorNicollet Installing ohm-tool
authored
151 (* Load (or update) the two parts of the Ohm framework (core and plugins) from
152 the github repository. *)
153
154 let () = List.iter (fun (path,src) -> Install.clone path src) [
155 [ ".ohm" ; "Ohm" ], "git://github.com/VictorNicollet/Ohm.git" ;
156 [ ".ohm" ; "Ohm-Plugins" ], "git://github.com/VictorNicollet/Ohm-Plugins.git" ;
157 ]
158
159 (* Build the framework *)
160
161 let () = Install.run "make --quiet -C %s" (Filename.quote (path [".ohm" ; "Ohm"]))
162
85f5182 @VictorNicollet Build plugins after install
authored
163 (* Build the plugins that need building. *)
164
165 let () = List.iter Install.make_plugin (readdir [".ohm" ; "Ohm-Plugins"])
166
34e307b @VictorNicollet Installing ohm-tool
authored
167 (* Create the relevant symlinks *)
168
169 let () = List.iter (fun (src,dest) -> Install.symlink src dest) [
170 [ "ocaml" ; "gen" ], [ "_build" ] ;
171 [ "ocaml" ; "ohm" ], [ ".ohm" ; "Ohm" ] ;
172 ]
173
174 (* Copy over files *)
175
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
176 let () = if fresh then List.iter
177 (fun path -> Install.copy ([".ohm";"Ohm";"install"]@path) path) [
178 [ "bot" ; "run" ] ;
179 [ "Makefile" ] ;
180 [ "ocaml" ; "myocamlbuild.ml" ] ;
181 [ "ocaml" ; "_tags" ] ;
182 [ "ocaml" ; "o.ml" ];
183 [ "ocaml" ; "main.ml" ] ;
9c6b3dc @VictorNicollet New install files
authored
184 [ "ocaml" ; "cErrorPage.mli" ] ;
185 [ "ocaml" ; "cErrorPage.ml" ] ;
66c8fed @VictorNicollet Install Error 500 page
authored
186 [ "ocaml" ; "configProject.mli" ] ;
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
187 [ "assets" ; "common" ; "def.adlib.ml" ] ;
188 [ "assets" ; "common" ; "en.adlib.ml" ] ;
9c6b3dc @VictorNicollet New install files
authored
189 [ "assets" ; "common" ; "style.css" ] ;
190 [ "assets" ; "errorPage" ; "error404.htm" ] ;
191 [ "assets" ; "errorPage" ; "style.css" ] ;
192 [ "assets" ; "errorPage" ; "def.adlib.ml" ] ;
193 [ "assets" ; "errorPage" ; "en.adlib.ml" ] ;
36c47d6 @VictorNicollet Move 500.htm
authored
194 [ "public" ; "500.htm" ]
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
195 ]
196
962d918 @VictorNicollet Generate project config
authored
197 (* Install the configuration file *)
0e098b5 @VictorNicollet Build project post-installation
authored
198
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
199 let () = if fresh then Install.config ()
962d918 @VictorNicollet Generate project config
authored
200
34e307b @VictorNicollet Installing ohm-tool
authored
201 (* Make files executable when appropriate *)
202
203 let () = List.iter Install.mkexec [
204 [ "bot" ; "run" ]
205 ]
0e098b5 @VictorNicollet Build project post-installation
authored
206
12a8a36 @VictorNicollet Difference between fresh installs and reinstalls
authored
207 (* Touch the "installed" file to avoid fresh installs from now on. *)
208
209 let () = if fresh then Install.touch [".install"]
210
0e098b5 @VictorNicollet Build project post-installation
authored
211 (* Finish install by compiling the software. *)
212
6bea165 @VictorNicollet Only clean up if non-fresh
authored
213 let () = Install.make fresh
Something went wrong with that request. Please try again.