| 
									
										
										
										
											2017-06-05 16:02:39 +03:00
										 |  |  | # Copyright (c) 2014-present PlatformIO <contact@platformio.org> | 
					
						
							| 
									
										
										
										
											2015-11-18 17:16:17 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | # you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | # You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #    http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | # See the License for the specific language governing permissions and | 
					
						
							|  |  |  | # limitations under the License. | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-12 15:14:58 +02:00
										 |  |  | import codecs | 
					
						
							| 
									
										
										
										
											2015-09-04 19:31:59 +03:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2018-12-13 17:24:08 +02:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import bottle | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  | from platformio import fs, util | 
					
						
							| 
									
										
										
										
											2019-05-27 22:25:22 +03:00
										 |  |  | from platformio.proc import where_is_program | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  | from platformio.project.helpers import load_project_ide_data | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ProjectGenerator(object): | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |     def __init__(self, config, env_name, ide): | 
					
						
							|  |  |  |         self.config = config | 
					
						
							|  |  |  |         self.project_dir = os.path.dirname(config.path) | 
					
						
							|  |  |  |         self.env_name = str(env_name) | 
					
						
							| 
									
										
										
										
											2019-06-03 17:44:41 +03:00
										 |  |  |         self.ide = str(ide) | 
					
						
							| 
									
										
										
										
											2016-07-09 18:44:45 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def get_supported_ides(): | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |         tpls_dir = os.path.join(fs.get_source_dir(), "ide", "tpls") | 
					
						
							|  |  |  |         return sorted( | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 d | 
					
						
							|  |  |  |                 for d in os.listdir(tpls_dir) | 
					
						
							|  |  |  |                 if os.path.isdir(os.path.join(tpls_dir, d)) | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-08-18 11:06:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 17:37:55 +03:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def filter_includes(includes_map, ignore_scopes=None, to_unix_path=True): | 
					
						
							|  |  |  |         ignore_scopes = ignore_scopes or [] | 
					
						
							|  |  |  |         result = [] | 
					
						
							|  |  |  |         for scope, includes in includes_map.items(): | 
					
						
							|  |  |  |             if scope in ignore_scopes: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             for include in includes: | 
					
						
							|  |  |  |                 if to_unix_path: | 
					
						
							|  |  |  |                     include = fs.to_unix_path(include) | 
					
						
							|  |  |  |                 if include not in result: | 
					
						
							|  |  |  |                     result.append(include) | 
					
						
							|  |  |  |         return result | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |     def _load_tplvars(self): | 
					
						
							| 
									
										
										
										
											2019-08-18 11:06:52 +03:00
										 |  |  |         tpl_vars = { | 
					
						
							|  |  |  |             "config": self.config, | 
					
						
							|  |  |  |             "systype": util.get_systype(), | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |             "project_name": os.path.basename(self.project_dir), | 
					
						
							| 
									
										
										
										
											2019-08-18 11:06:52 +03:00
										 |  |  |             "project_dir": self.project_dir, | 
					
						
							|  |  |  |             "env_name": self.env_name, | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |             "user_home_dir": os.path.realpath(fs.expanduser("~")), | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             "platformio_path": sys.argv[0] | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |             if os.path.isfile(sys.argv[0]) | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             else where_is_program("platformio"), | 
					
						
							| 
									
										
										
										
											2019-08-19 14:43:39 +03:00
										 |  |  |             "env_path": os.getenv("PATH"), | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             "env_pathsep": os.pathsep, | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-18 11:06:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |         # default env configuration | 
					
						
							| 
									
										
										
										
											2019-08-18 11:06:52 +03:00
										 |  |  |         tpl_vars.update(self.config.items(env=self.env_name, as_dict=True)) | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |         # build data | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         tpl_vars.update(load_project_ide_data(self.project_dir, self.env_name) or {}) | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  |         with fs.cd(self.project_dir): | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |             tpl_vars.update( | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     "src_files": self.get_src_files(), | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |                     "project_src_dir": self.config.get_optional_dir("src"), | 
					
						
							|  |  |  |                     "project_lib_dir": self.config.get_optional_dir("lib"), | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |                     "project_libdeps_dir": os.path.join( | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |                         self.config.get_optional_dir("libdeps"), self.env_name | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |                     ), | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2019-08-19 14:43:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for key, value in tpl_vars.items(): | 
					
						
							|  |  |  |             if key.endswith(("_path", "_dir")): | 
					
						
							| 
									
										
										
										
											2019-08-27 20:21:53 +03:00
										 |  |  |                 tpl_vars[key] = fs.to_unix_path(value) | 
					
						
							| 
									
										
										
										
											2020-04-21 17:37:55 +03:00
										 |  |  |         for key in ("src_files", "libsource_dirs"): | 
					
						
							| 
									
										
										
										
											2019-08-19 14:43:39 +03:00
										 |  |  |             if key not in tpl_vars: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2019-08-27 20:21:53 +03:00
										 |  |  |             tpl_vars[key] = [fs.to_unix_path(inc) for inc in tpl_vars[key]] | 
					
						
							| 
									
										
										
										
											2019-08-19 14:43:39 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-23 23:13:48 +03:00
										 |  |  |         tpl_vars["to_unix_path"] = fs.to_unix_path | 
					
						
							| 
									
										
										
										
											2020-04-21 17:37:55 +03:00
										 |  |  |         tpl_vars["filter_includes"] = self.filter_includes | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |         return tpl_vars | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-13 14:01:16 +01:00
										 |  |  |     def get_src_files(self): | 
					
						
							| 
									
										
										
										
											2015-05-07 17:21:44 +03:00
										 |  |  |         result = [] | 
					
						
							| 
									
										
										
										
											2019-08-12 19:44:37 +03:00
										 |  |  |         with fs.cd(self.project_dir): | 
					
						
							| 
									
										
										
										
											2019-09-27 14:13:53 +03:00
										 |  |  |             for root, _, files in os.walk(self.config.get_optional_dir("src")): | 
					
						
							| 
									
										
										
										
											2015-06-27 21:18:33 +03:00
										 |  |  |                 for f in files: | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |                     result.append(os.path.relpath(os.path.join(root, f))) | 
					
						
							| 
									
										
										
										
											2015-05-07 17:21:44 +03:00
										 |  |  |         return result | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  |     def get_tpls(self): | 
					
						
							| 
									
										
										
										
											2015-07-15 19:34:10 +03:00
										 |  |  |         tpls = [] | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |         tpls_dir = os.path.join(fs.get_source_dir(), "ide", "tpls", self.ide) | 
					
						
							| 
									
										
										
										
											2015-09-04 19:31:59 +03:00
										 |  |  |         for root, _, files in os.walk(tpls_dir): | 
					
						
							| 
									
										
										
										
											2015-07-15 19:34:10 +03:00
										 |  |  |             for f in files: | 
					
						
							| 
									
										
										
										
											2015-09-04 19:31:59 +03:00
										 |  |  |                 if not f.endswith(".tpl"): | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 _relpath = root.replace(tpls_dir, "") | 
					
						
							|  |  |  |                 if _relpath.startswith(os.sep): | 
					
						
							| 
									
										
										
										
											2015-09-04 20:35:56 +03:00
										 |  |  |                     _relpath = _relpath[1:] | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |                 tpls.append((_relpath, os.path.join(root, f))) | 
					
						
							| 
									
										
										
										
											2015-07-15 19:34:10 +03:00
										 |  |  |         return tpls | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def generate(self): | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |         tpl_vars = self._load_tplvars() | 
					
						
							| 
									
										
										
										
											2016-07-05 13:18:31 +03:00
										 |  |  |         for tpl_relpath, tpl_path in self.get_tpls(): | 
					
						
							|  |  |  |             dst_dir = self.project_dir | 
					
						
							|  |  |  |             if tpl_relpath: | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |                 dst_dir = os.path.join(self.project_dir, tpl_relpath) | 
					
						
							|  |  |  |                 if not os.path.isdir(dst_dir): | 
					
						
							| 
									
										
										
										
											2016-07-05 13:18:31 +03:00
										 |  |  |                     os.makedirs(dst_dir) | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |             file_name = os.path.basename(tpl_path)[:-4] | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |             contents = self._render_tpl(tpl_path, tpl_vars) | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |             self._merge_contents(os.path.join(dst_dir, file_name), contents) | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 19:20:10 +03:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def _render_tpl(tpl_path, tpl_vars): | 
					
						
							| 
									
										
										
										
											2020-02-12 15:14:58 +02:00
										 |  |  |         with codecs.open(tpl_path, "r", encoding="utf8") as fp: | 
					
						
							| 
									
										
										
										
											2020-04-27 23:41:36 +03:00
										 |  |  |             return bottle.template(fp.read(), **tpl_vars) | 
					
						
							| 
									
										
										
										
											2015-04-24 14:43:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-05 13:28:22 +03:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def _merge_contents(dst_path, contents): | 
					
						
							| 
									
										
										
										
											2020-06-12 23:47:12 +03:00
										 |  |  |         if os.path.basename(dst_path) == ".gitignore" and os.path.isfile(dst_path): | 
					
						
							| 
									
										
										
										
											2018-10-27 14:20:33 +03:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2020-02-12 15:14:58 +02:00
										 |  |  |         with codecs.open(dst_path, "w", encoding="utf8") as fp: | 
					
						
							| 
									
										
										
										
											2019-06-03 17:44:41 +03:00
										 |  |  |             fp.write(contents) |