Skip to content

Commit

Permalink
Avoid blowing away .mbedignore files in exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherjimmy committed Feb 16, 2018
1 parent ccff46d commit 8d7311d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tools/export/e2studio/__init__.py
Expand Up @@ -37,5 +37,5 @@ def generate(self):
self.gen_file('e2studio/launch.tmpl', jinja_ctx, '%s OpenOCD.launch' % self.project_name, trim_blocks=True, lstrip_blocks=True)

self.gen_file('gnuarmeclipse/.project.tmpl', jinja_ctx, '.project', trim_blocks=True, lstrip_blocks=True)
self.gen_file('gnuarmeclipse/mbedignore.tmpl', jinja_ctx, '.mbedignore')
self.gen_file_nonoverwrite('gnuarmeclipse/mbedignore.tmpl', jinja_ctx, '.mbedignore')
self.gen_file('gnuarmeclipse/makefile.targets.tmpl', jinja_ctx, 'makefile.targets', trim_blocks=True, lstrip_blocks=True)
30 changes: 29 additions & 1 deletion tools/export/exporters.py
Expand Up @@ -2,7 +2,7 @@
import os
from abc import abstractmethod, ABCMeta
import logging
from os.path import join, dirname, relpath, basename, realpath, normpath
from os.path import join, dirname, relpath, basename, realpath, normpath, exists
from itertools import groupby
from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment
Expand Down Expand Up @@ -130,7 +130,34 @@ def get_source_paths(self):
source_files.extend(getattr(self.resources, key))
return list(set([os.path.dirname(src) for src in source_files]))

def gen_file_dest(self, target_file):
"""Generate the project file location in an exported project"""
return join(self.export_dir, target_file)

def gen_file(self, template_file, data, target_file, **kwargs):
"""Generates a project file from a template using jinja"""
target_text = self._gen_file_inner(template_file, data, target_file, **kwargs)
target_path = self.gen_file_dest(target_file)
logging.debug("Generating: %s", target_path)
open(target_path, "w").write(target_text)
self.generated_files += [target_path]

def gen_file_nonoverwrite(self, template_file, data, target_file, **kwargs):
"""Generates a project file from a template using jinja"""
target_text = self._gen_file_inner(template_file, data, target_file, **kwargs)
target_path = self.gen_file_dest(target_file)
if exists(target_path):
with open(target_path) as fdin:
old_text = fdin.read()
if target_text not in old_text:
with open(target_path, "a") as fdout:
fdout.write(target_text)
else:
logging.debug("Generating: %s", target_path)
open(target_path, "w").write(target_text)
self.generated_files += [target_path]

def _gen_file_inner(self, template_file, data, target_file, **kwargs):
"""Generates a project file from a template using jinja"""
jinja_loader = FileSystemLoader(
os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -139,6 +166,7 @@ def gen_file(self, template_file, data, target_file, **kwargs):

template = jinja_environment.get_template(template_file)
target_text = template.render(data)
return target_text

target_path = join(self.export_dir, target_file)
logging.debug("Generating: %s", target_path)
Expand Down
3 changes: 2 additions & 1 deletion tools/export/gnuarmeclipse/__init__.py
Expand Up @@ -293,7 +293,8 @@ def generate(self):
'.cproject', trim_blocks=True, lstrip_blocks=True)
self.gen_file('gnuarmeclipse/makefile.targets.tmpl', jinja_ctx,
'makefile.targets', trim_blocks=True, lstrip_blocks=True)
self.gen_file('gnuarmeclipse/mbedignore.tmpl', jinja_ctx, '.mbedignore')
self.gen_file_nonoverwrite('gnuarmeclipse/mbedignore.tmpl', jinja_ctx,
'.mbedignore')

print
print 'Done. Import the \'{0}\' project in Eclipse.'.format(self.project_name)
Expand Down
3 changes: 2 additions & 1 deletion tools/export/mcuxpresso/__init__.py
Expand Up @@ -224,7 +224,8 @@ def generate(self):
'.cproject', trim_blocks=True, lstrip_blocks=True)
self.gen_file('mcuxpresso/makefile.targets.tmpl', jinja_ctx,
'makefile.targets', trim_blocks=True, lstrip_blocks=True)
self.gen_file('mcuxpresso/mbedignore.tmpl', jinja_ctx, '.mbedignore')
self.gen_file_nonoverwrite('mcuxpresso/mbedignore.tmpl', jinja_ctx,
'.mbedignore')

print
print 'Done. Import the \'{0}\' project in Eclipse.'.format(self.project_name)
Expand Down
3 changes: 2 additions & 1 deletion tools/export/nb/__init__.py
Expand Up @@ -268,7 +268,8 @@ def generate(self):

self.gen_file('nb/configurations.tmpl', jinja_ctx, 'nbproject/configurations.xml')
self.gen_file('nb/project.tmpl', jinja_ctx, 'nbproject/project.xml')
self.gen_file('nb/mbedignore.tmpl', jinja_ctx, '.mbedignore')
self.gen_file_nonoverwrite('nb/mbedignore.tmpl', jinja_ctx,
'.mbedignore')
self.gen_file('nb/Makefile.tmpl', jinja_ctx, 'Makefile')

print
Expand Down

0 comments on commit 8d7311d

Please sign in to comment.