nitpretty: move documentation to man page.
[nit.git] / src / nitpretty.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # `nitpretty` is a tool able to pretty print Nit files.
16 #
17 # See `man nitpretty` for more infos.
18 import pretty
19
20 redef class ToolContext
21 # The working directory used to store temp files.
22 var opt_dir = new OptionString("Working directory (default is '.nitpretty')", "--dir")
23
24 # Output pretty printed code with this filename.
25 var opt_output = new OptionString("Output name (default is pretty.nit)", "-o",
26 "--output")
27
28 # Show diff between source and pretty printed code.
29 var opt_diff = new OptionBool("Show diff between source and output", "--diff")
30
31 # Show diff between source and pretty printed code using meld.
32 var opt_meld = new OptionBool("Show diff between source and output using meld",
33 "--meld")
34
35 # Check formatting instead of pretty printing.
36 #
37 # This option create a tempory pretty printed file then check if
38 # the output of the diff command on the source file and the pretty
39 # printed one is empty.
40 var opt_check = new OptionBool("Check format of Nit source files", "--check")
41 end
42
43 # Return result from diff between `file1` and `file2`.
44 private fun diff(file1, file2: String): String do
45 var p = new IProcess("diff", "-u", file1, file2)
46 var res = p.read_all
47 p.wait
48 p.close
49 return res
50 end
51
52 # process options
53 var toolcontext = new ToolContext
54
55 toolcontext.option_context.
56 add_option(toolcontext.opt_dir, toolcontext.opt_output, toolcontext.opt_diff,
57 toolcontext.opt_meld, toolcontext.opt_check)
58
59 toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
60 "Pretty print Nit code from Nit source files."
61
62 toolcontext.process_options args
63 var arguments = toolcontext.option_context.rest
64 # build model
65 var model = new Model
66 var mbuilder = new ModelBuilder(model, toolcontext)
67 var mmodules = mbuilder.parse(arguments)
68 mbuilder.run_phases
69
70 if mmodules.is_empty then
71 print "Error: no module to pretty print"
72 return
73 end
74
75 if not toolcontext.opt_check.value and mmodules.length > 1 then
76 print "Error: only --check option allow multiple modules"
77 return
78 end
79
80 var dir = toolcontext.opt_dir.value or else ".nitpretty"
81 if not dir.file_exists then dir.mkdir
82 var v = new PrettyPrinterVisitor
83
84 for mmodule in mmodules do
85 if not mbuilder.mmodule2nmodule.has_key(mmodule) then
86 print " Error: no source file for module {mmodule}"
87 return
88 end
89
90 var nmodule = mbuilder.mmodule2nmodule[mmodule]
91 var file = "{dir}/{mmodule.name}.nit"
92 var tpl = v.pretty_nmodule(nmodule)
93 tpl.write_to_file file
94
95 if toolcontext.opt_check.value then
96 var res = diff(nmodule.location.file.filename, file)
97
98 if not res.is_empty then
99 print "Wrong formating for module {nmodule.location.file.filename}"
100 toolcontext.info(res, 1)
101
102 if toolcontext.opt_meld.value then
103 sys.system "meld {nmodule.location.file.filename} {file}"
104 end
105 else
106 toolcontext.info("[OK] {nmodule.location.file.filename}", 1)
107 end
108 else
109 # write to file
110 var out = toolcontext.opt_output.value
111 if out != null then sys.system "cp {file} {out}"
112
113 # open in meld
114 if toolcontext.opt_meld.value then
115 sys.system "meld {arguments.first} {file}"
116 return
117 end
118
119 # show diff
120 if toolcontext.opt_diff.value then
121 var res = diff(arguments.first, file)
122 if not res.is_empty then print res
123 return
124 end
125
126 # show pretty
127 if not toolcontext.opt_quiet.value then tpl.write_to sys.stdout
128 end
129 end