Skip to content
Browse files

[bockbuild] Add options to write ENV via xml

Environment variables used/set by bockbuild can now be dumpe on the
commandline via --csproj-env or directly added to a MonoDevelop .csproj
file via --csproj-insert=<file>
  • Loading branch information...
1 parent 95aebbe commit 331f6c33f041251fc8165eddf213480db1730dfa @Dynalon Dynalon committed
View
2 bockbuild/darwinprofile.py
@@ -1,7 +1,7 @@
import os
import shutil
from plistlib import Plist
-from util import *
+from util.util import *
from unixprofile import UnixProfile
class DarwinProfile (UnixProfile):
View
11 bockbuild/environment.py
@@ -1,5 +1,6 @@
import os
-from util import *
+from util.util import *
+from util.csproj import *
from collections import deque
class EnvironmentItem:
@@ -39,6 +40,14 @@ def dump (self):
for k in self.get_names ():
print 'export %s="%s"' % (k, self.__dict__[k])
+ def dump_csproj (self):
+ for k in self.get_names ():
+ print '<Variable name="%s" value="%s" />' % (k, self.__dict__[k])
+
+ def write_csproj (self, file):
+ writer = csproj_writer (file, self)
+ writer.write ()
+
def export (self):
for k in self.get_names ():
os.environ[k] = str (self.__dict__[k])
View
2 bockbuild/glickprofile.py
@@ -1,7 +1,7 @@
import os
import shutil
from plistlib import Plist
-from util import *
+from util.util import *
from unixprofile import UnixProfile
class GlickProfile (UnixProfile):
View
2 bockbuild/package.py
@@ -2,7 +2,7 @@
import sys
import shutil
from urllib import FancyURLopener
-from util import *
+from util.util import *
class Package:
def __init__ (self, name, version, configure_flags = None, sources = None, source_dir_name = None, override_properties = None):
View
24 bockbuild/profile.py
@@ -1,6 +1,7 @@
import os
from optparse import OptionParser
-from util import *
+from util.util import *
+from util.csproj import *
from environment import Environment
from package import *
@@ -58,6 +59,12 @@ def parse_options (self):
parser.add_option ('-p', '--show-source-paths', default = False,
action = 'store_true', dest = 'show_source_paths',
help = 'Output a list of all source paths/URLs for all packages')
+ parser.add_option ('', '--csproj-env', default = False,
+ action = 'store_true', dest = 'dump_environment_csproj',
+ help = 'Dump the profile environment xml formarted for use in .csproj files')
+ parser.add_option ('', '--csproj-insert', default = None,
+ action = 'store', dest = 'csproj_file',
+ help = 'Inserts the profile environment variables into VS/MonoDevelop .csproj files')
self.parser = parser
self.cmd_options, self.cmd_args = parser.parse_args ()
@@ -78,6 +85,21 @@ def build (self):
self.env.dump ()
sys.exit (0)
+ if self.cmd_options.dump_environment_csproj:
+ # specify to use our GAC, else MonoDevelop would
+ # use its own
+ self.env.set ('MONO_GAC_PREFIX', self.prefix)
+
+ self.env.compile ()
+ self.env.dump_csproj ()
+ sys.exit (0)
+
+ if self.cmd_options.csproj_file is not None:
+ self.env.set ('MONO_GAC_PREFIX', self.prefix)
+ self.env.compile ()
+ self.env.write_csproj (self.cmd_options.csproj_file)
+ sys.exit (0)
+
if not self.cmd_options.show_source_paths and \
not self.cmd_options.do_build and \
not self.cmd_options.do_bundle and \
View
0 bockbuild/util/__init__.py
No changes.
View
91 bockbuild/util/csproj.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+# coding: utf8
+
+import shutil
+import io
+
+from xml.etree import ElementTree
+from xml.etree.ElementTree import Element
+from xml.etree.ElementTree import Comment
+
+class csproj_writer:
+
+ def __init__ (self, file, vars, condition = "Debug|AnyCPU"):
+
+ self.vars = vars
+ self.file = file
+
+ self.tree = ElementTree.parse (file)
+
+ # get the namespace from the root Project element
+ self.ns = self.tree.getroot().tag[1:].split("}")[0]
+
+ # the python elementtree always puts a prefix,
+ # hardcode it so we can substitute it later
+ ElementTree.register_namespace("csproj0", self.ns)
+
+ # find PropertyGroup
+ group = self.tree.findall (".//{%s}PropertyGroup[@Condition]" % self.ns)
+
+ for node in group:
+ # only insert into Debug|AnyCPU
+ if condition in (node.get ("Condition")):
+ self.insert_env_if_missing (node)
+ self.substitute_env_var (node)
+
+ def substitute_env_var (self, propertygroup):
+ node = propertygroup.find ("./{%s}EnvironmentVariables/{%s}EnvironmentVariables" % (self.ns, self.ns))
+
+ for name in self.vars.get_names ():
+
+ value = self.vars.__dict__[name]
+
+ # check if variable is already set
+ var = node.find ("{%s}Variable[@name='%s']" % (self.ns, name))
+ if var is not None:
+ # update its value
+ var.set ('value', "%s" % value)
+ else:
+ # insert new node
+ el = Element ("{%s}Variable" % self.ns)
+ el.set ('name', name)
+ el.set ('value', "%s" % value)
+ self.insert (node, 0, el)
+
+ def insert_env_if_missing (self, node):
+ # test if environment variable is present - it is usually double wrapped
+ outer = node.find ("{%s}EnvironmentVariables" % self.ns)
+ if outer is not None:
+ # see if the inner node is present, too
+ inner = node.find ("{%s}EnvironmentVariables" % self.ns)
+ if inner is None:
+ inner = Element ("{%s}EnvironmentVariables" % self.ns)
+ self.insert (outer, 0, inner)
+
+ else:
+ outer = Element ("{%s}EnvironmentVariables" % self.ns)
+ inner = Element ("{%s}EnvironmentVariables" % self.ns)
+ #self.insert (outer, 0, Comment ("AUTO GENERATED VARIABLES - DO NOT INCLUDE IN ANY GIT COMMITS!"))
+ self.insert (node, 1, outer)
+ self.insert (outer, 0, inner)
+
+ # wrapper arround Element.insert that appends a linebreak comment
+ # HACK etree xml library can not pretty print
+ def insert (self, node, pos, element):
+ brcomment = Comment ("REPLACE_WITH_LINEBREAK");
+ node.insert (pos, brcomment)
+ node.insert (pos, element)
+
+ def write (self):
+
+ xml = ElementTree.tostring(self.tree.getroot (), encoding="utf-8")
+
+ # HACK the python xml library is nuts - manually remove the forced namespace
+ # prefix and re-establish a minimal form of pretty printing
+ xml = xml.replace ("csproj0:","")
+ xml = xml.replace ("xmlns:csproj0","xmlns")
+ xml = xml.replace ("<!--REPLACE_WITH_LINEBREAK-->","\n")
+
+ f = open (self.file, 'w')
+ f.write(xml)
+
View
0 bockbuild/util.py → bockbuild/util/util.py
File renamed without changes.

0 comments on commit 331f6c3

Please sign in to comment.
Something went wrong with that request. Please try again.