diff --git a/HISTORY.rst b/HISTORY.rst index 83892644..b381be6b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -11,6 +11,7 @@ PlatformIO 2.0 (from EEMEM directive) * Improved project generator for `CLion IDE `__ * Auto-remove project cache when PlatformIO is upgraded +* Keep user changes for ``.gitignore`` file when re-generate/update project data * Fixed missed ``--boot`` flag for the firmware uploader for ATSAM3X8E Cortex-M3 MCU based boards (Arduino Due, etc) (`issue #710 `_) diff --git a/platformio/ide/projectgenerator.py b/platformio/ide/projectgenerator.py index 3347ae8d..53f776bd 100644 --- a/platformio/ide/projectgenerator.py +++ b/platformio/ide/projectgenerator.py @@ -16,8 +16,8 @@ import json import os import re import sys -from os.path import (abspath, basename, expanduser, isdir, join, normpath, - relpath) +from os.path import (abspath, basename, expanduser, isdir, isfile, join, + normpath, relpath) import bottle @@ -113,16 +113,18 @@ class ProjectGenerator(object): return tpls def generate(self): - for _relpath, _path in self.get_tpls(): - tpl_dir = self.project_dir - if _relpath: - tpl_dir = join(self.project_dir, _relpath) - if not isdir(tpl_dir): - os.makedirs(tpl_dir) + for tpl_relpath, tpl_path in self.get_tpls(): + dst_dir = self.project_dir + if tpl_relpath: + dst_dir = join(self.project_dir, tpl_relpath) + if not isdir(dst_dir): + os.makedirs(dst_dir) - file_name = basename(_path)[:-4] - with open(join(tpl_dir, file_name), "w") as f: - f.write(self._render_tpl(_path).encode("utf8")) + file_name = basename(tpl_path)[:-4] + self._merge_contents( + join(dst_dir, file_name), + self._render_tpl(tpl_path).encode("utf8") + ) def _render_tpl(self, tpl_path): content = "" @@ -130,6 +132,22 @@ class ProjectGenerator(object): content = f.read() return bottle.template(content, **self._tplvars) + def _merge_contents(self, dst_path, contents): + file_name = basename(dst_path) + + # merge .gitignore + if file_name == ".gitignore" and isfile(dst_path): + contents = [l.strip() for l in contents.split("\n") if l.strip()] + with open(dst_path) as f: + for line in f.readlines(): + line = line.strip() + if line and line not in contents: + contents.append(line) + contents = "\n".join(contents) + + with open(dst_path, "w") as f: + f.write(contents) + def _gather_tplvars(self): self._tplvars.update(self.get_project_env()) self._tplvars.update(self.get_project_build_data())