1 | |
---|
2 | #include "cmt_system.h" |
---|
3 | #include "cmt.h" |
---|
4 | #include "cmt_install_area.h" |
---|
5 | #include "cmt_syntax.h" |
---|
6 | #include "cmt_use.h" |
---|
7 | #include "cmt_symbol.h" |
---|
8 | #include "cmt_cmtpath_pattern.h" |
---|
9 | |
---|
10 | CmtInstallAreaMgr& CmtInstallAreaMgr::instance () |
---|
11 | { |
---|
12 | static CmtInstallAreaMgr me; |
---|
13 | |
---|
14 | return (me); |
---|
15 | } |
---|
16 | |
---|
17 | void CmtInstallAreaMgr::setup_current_install_area () |
---|
18 | { |
---|
19 | m_install_area = build_current_install_area (); |
---|
20 | |
---|
21 | if (m_install_area == "") return; |
---|
22 | |
---|
23 | cmt_string installarea_prefix; |
---|
24 | |
---|
25 | Symbol* macro = Symbol::find ("cmt_installarea_prefix"); |
---|
26 | if (macro != 0) |
---|
27 | { |
---|
28 | installarea_prefix = macro->build_macro_value (); |
---|
29 | } |
---|
30 | |
---|
31 | if (installarea_prefix != "") |
---|
32 | { |
---|
33 | m_install_area += CmtSystem::file_separator (); |
---|
34 | m_install_area += installarea_prefix; |
---|
35 | } |
---|
36 | |
---|
37 | Use& current_use = Use::current (); |
---|
38 | cmt_string buffer; |
---|
39 | |
---|
40 | if (!Symbol::is_selected ("CMTINSTALLAREA")) |
---|
41 | { |
---|
42 | buffer = "set CMTINSTALLAREA \""; |
---|
43 | buffer += m_install_area; |
---|
44 | buffer += "\""; |
---|
45 | |
---|
46 | SyntaxParser::parse_requirements_line (buffer, ¤t_use); |
---|
47 | buffer = ""; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | void CmtInstallAreaMgr::setup () |
---|
52 | { |
---|
53 | m_install_area = build_current_install_area (); |
---|
54 | |
---|
55 | setup_current_install_area (); |
---|
56 | |
---|
57 | CmtPathPattern::apply_all (); |
---|
58 | } |
---|
59 | |
---|
60 | void CmtInstallAreaMgr::config () const |
---|
61 | { |
---|
62 | cmt_string installarea = build_current_install_area (); |
---|
63 | |
---|
64 | CmtPathPattern::apply_all (); |
---|
65 | |
---|
66 | if (installarea == "") return; |
---|
67 | |
---|
68 | // cout << "InstallArea installed in " << installarea << endl; |
---|
69 | |
---|
70 | if (!Cmt::get_quiet ()) |
---|
71 | { |
---|
72 | cout << "# Doing cleanup in the installation area " << installarea << endl; |
---|
73 | } |
---|
74 | |
---|
75 | Symbol* macro = Symbol::find ("cmt_installarea_paths"); |
---|
76 | if (macro == 0) return; |
---|
77 | cmt_string installarea_paths = macro->build_macro_value (); |
---|
78 | |
---|
79 | CmtSystem::cmt_string_vector areapaths; |
---|
80 | CmtSystem::split (installarea_paths, " \t", areapaths); |
---|
81 | |
---|
82 | // Try a cleanup only in this selected install area |
---|
83 | |
---|
84 | for (int i = 0; i < areapaths.size (); i++) |
---|
85 | { |
---|
86 | const cmt_string& p = areapaths[i]; |
---|
87 | |
---|
88 | cmt_string path = installarea; |
---|
89 | path += CmtSystem::file_separator (); |
---|
90 | path += p; |
---|
91 | |
---|
92 | Symbol::expand (path); |
---|
93 | |
---|
94 | path.replace_all ("/", CmtSystem::file_separator ()); |
---|
95 | path.replace_all ("\\", CmtSystem::file_separator ()); |
---|
96 | |
---|
97 | CmtSystem::cmt_string_vector refs; |
---|
98 | cmt_regexp expression (".*[.]cmtref$"); |
---|
99 | |
---|
100 | // Look for all cmtref files in this PATH pattern |
---|
101 | |
---|
102 | CmtSystem::scan_dir (path, expression, refs); |
---|
103 | |
---|
104 | for (int j = 0; j < refs.size (); j++) |
---|
105 | { |
---|
106 | const cmt_string& ref_file = refs[j]; |
---|
107 | |
---|
108 | // We get the absolute location of the installed file |
---|
109 | |
---|
110 | cmt_string ref; |
---|
111 | ref.read (ref_file); |
---|
112 | int pos; |
---|
113 | |
---|
114 | ref.replace_all ("\"", ""); |
---|
115 | |
---|
116 | if (Cmt::get_debug ()) |
---|
117 | { |
---|
118 | cout << "CmtInstallAreaMgr::config> " << ref_file << " " << ref << endl; |
---|
119 | } |
---|
120 | |
---|
121 | pos = ref.find ("\r\n"); |
---|
122 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
123 | pos = ref.find ('\n'); |
---|
124 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
125 | pos = ref.find ('\r'); |
---|
126 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
127 | pos = ref.find (' '); |
---|
128 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
129 | |
---|
130 | // If the referenced file cannot be reached we remove the |
---|
131 | // corresponding installation |
---|
132 | // (this happens if the referenced file has be removed, or |
---|
133 | // moved away) |
---|
134 | |
---|
135 | if (!CmtSystem::test_file (ref)) |
---|
136 | { |
---|
137 | cmt_string ref_name; |
---|
138 | |
---|
139 | // Get the name of the referenced file |
---|
140 | CmtSystem::basename (ref, ref_name); |
---|
141 | |
---|
142 | // Get the installation directory |
---|
143 | CmtSystem::dirname (ref_file, ref); |
---|
144 | |
---|
145 | ref += CmtSystem::file_separator (); |
---|
146 | ref += ref_name; |
---|
147 | |
---|
148 | // Remove both the installed file |
---|
149 | // and the reference file |
---|
150 | |
---|
151 | if (!Cmt::get_quiet ()) |
---|
152 | { |
---|
153 | cout << "# Removing obsolete installed file [" << ref << "]"; |
---|
154 | cout << " (and " << ref_file << ")" << endl; |
---|
155 | } |
---|
156 | |
---|
157 | CmtSystem::remove_file (ref); |
---|
158 | CmtSystem::remove_file (ref_file); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | const cmt_string& CmtInstallAreaMgr::get_install_area () const |
---|
165 | { |
---|
166 | return (m_install_area); |
---|
167 | } |
---|
168 | |
---|
169 | cmt_string CmtInstallAreaMgr::build_current_install_area () const |
---|
170 | { |
---|
171 | cmt_string installarea; |
---|
172 | |
---|
173 | const cmt_string pwd = CmtSystem::pwd (); |
---|
174 | |
---|
175 | installarea = Cmt::find_in_cmt_paths (pwd); |
---|
176 | |
---|
177 | return (installarea); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | |
---|