1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Helpful features about packages
20 redef class ToolContext
22 var opt_expand
= new OptionBool("Move singleton packages to their own directory", "--expand")
24 # README handling phase
25 var readme_phase
: Phase = new ReadmePhase(self, null)
29 option_context
.add_option
(opt_expand
)
33 private class ReadmePhase
36 redef fun process_mainmodule
(mainmodule
, mmodules
) do
37 var expand_packages
= toolcontext
.opt_expand
.value
38 var model
= toolcontext
.modelbuilder
.model
40 for mpackage
in model
.mpackages
do
42 # Fictive and buggy packages are ignored
43 if not mpackage
.has_source
then
44 toolcontext
.warning
(mpackage
.location
, "no-source",
45 "Warning: `{mpackage}` has no source file")
50 if not mpackage
.is_expanded
then
51 if expand_packages
then
52 var path
= mpackage
.expand
53 toolcontext
.info
("{mpackage} moved to {path}", 0)
55 toolcontext
.warning
(mpackage
.location
, "no-dir",
56 "Warning: `{mpackage}` has no package directory")
66 # Does `self` have a source file?
67 private var has_source
: Bool is lazy
do
68 return location
.file
!= null
71 # Is `self` in its own directory?
72 private var is_expanded
: Bool is lazy
do
73 if not has_source
then return false
74 var path
= location
.file
.as(not null).filename
75 if path
.has_suffix
(".nit") then return false
79 # Expand `self` in its own directory
80 private fun expand
: String do
81 assert not is_expanded
83 var ori_path
= location
.file
.as(not null).filename
84 var new_path
= ori_path
.dirname
/ name
87 sys
.system
"mv {ori_path} {new_path / name}.nit"
89 var ini_file
= "{new_path}.ini"
90 if ini_file
.file_exists
then
91 sys
.system
"mv {new_path}.ini {new_path}/package.ini"
99 var toolcontext
= new ToolContext
100 var tpl
= new Template
101 tpl
.add
"Usage: nitpackage [OPTION]... <file.nit>...\n"
102 tpl
.add
"Helpful features about packages."
103 toolcontext
.tooldescription
= tpl
.write_to_string
106 toolcontext
.process_options
(args
)
107 var arguments
= toolcontext
.option_context
.rest
110 var model
= new Model
111 var mbuilder
= new ModelBuilder(model
, toolcontext
)
112 var mmodules
= mbuilder
.parse_full
(arguments
)
115 if mmodules
.is_empty
then return
117 toolcontext
.run_global_phases
(mmodules
)