Esempio n. 1
0
    def make_nested_projects(folders):
        """Return a string representing all the nested projects
       in the .sln file

    It works by iterating over the folders and populating
    the nested_project template with the guids of each nested project
    and its containing folder.

    Each folder is an object that has guid attribute and a projects
    attribute (which is a list of its contained projects)
    """
        title()
        for f in folders.values():
            assert hasattr(f, 'guid') and type(f.guid) == str
            assert hasattr(f, 'projects') and type(f.projects) in (list, tuple)

        result = ''
        nested_project = '\t\t${GUID} = ${FolderGUID}\n'
        t = string.Template(nested_project)
        for folder in folders.values():
            for p in folder.projects:
                d = dict(GUID=p.guid, FolderGUID=folder.guid)
                s = t.substitute(d)
                result += s

        return result[:-1]
Esempio n. 2
0
    def make_projects(source_dir, projects):
        """Return a string representing all the projects in the .sln file

    It works by iterating over the projects and populating
    the project_template. Note that some projects are actually just
    solution folders. It is the responsibility of the caller to pass
    proper project objects that has type, name, filename, guid and dependencies
    attributes.
    """
        title()
        result = ''
        t1 = string.Template(project_template_with_dependencies)
        t2 = string.Template(project_template_without_dependencies)
        for p in projects:
            if p.type == project_type:
                filename = p.path[len(source_dir) + 1:].replace('/', '\\')
            else:
                filename = p.name
            dependency_guids = [get_guid(p.path) for d in p.dependencies]
            guid = get_guid(filename) if p.guid is None else p.guid
            d = dict(TypeGUID=p.type,
                     Name=p.name,
                     Filename=filename,
                     GUID=guid,
                     ProjectDependencies=make_project_dependencies(
                         p.dependencies))
            t = t1 if p.dependencies != [] else t2
            s = t.substitute(d)
            result += s

        return result[:-1]
Esempio n. 3
0
    def make_configurations(projects):
        """Return a string representing all the project configuration platforms
       in the .sln file

    It works by iterating over the projects and populating
    the project_configuration_platform template.
    """
        title()
        result = ''
        t = string.Template(project_configuration_platform_template)
        for p in projects:
            d = dict(GUID=p.guid)
            s = t.substitute(d)
            result += s

        return result[:-1]
Esempio n. 4
0
    def get_existing_folders(sln_filename):
        """Return the names and guids of current folders

    This is useful for generating a .sln file with identical guids.
    """
        title()
        lines = open(sln_filename).readlines()
        results = {}
        for line in lines:
            if line.startswith(
                    'Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") ='):
                tokens = line.split('"')
                #print tokens
                name = tokens[-4]
                guid = tokens[-2]
                results[name] = guid

        return results
Esempio n. 5
0
def make_solution(source_dir, folders):
    """Return a string representing the .sln file

  It uses a lot of nested functions to make the different parts
  of a solution file:
   - make_project_dependencies
   - make_projects
   - make_configurations
   - make nested_projects

  @param folders - a dictionary whose keys are VS folders and the values
    are the projects each folder contains. Each project must be an object that
    has a directory path (relative to the root dir), a guid and a
    list of dependencies (each dependency is another projects). This directory
    should contain a .vcproj file whose name matches the directory name.
  @param projects - a list of projects that don't have a folder and are contained
    directly by the solution node.
  """
    def get_existing_folders(sln_filename):
        """Return the names and guids of current folders

    This is useful for generating a .sln file with identical guids.
    """
        title()
        lines = open(sln_filename).readlines()
        results = {}
        for line in lines:
            if line.startswith(
                    'Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") ='):
                tokens = line.split('"')
                #print tokens
                name = tokens[-4]
                guid = tokens[-2]
                results[name] = guid

        return results

    def make_project_dependencies(dependency_guids):
        """Return a string representing all the dependencies in
    a ProjectDependencies sub-section

    @param dependency_guids - A list of GUIDs of all the dependencies
    """
        #title()
        if dependency_guids == []:
            return ''

        result = []
        for g in dependency_guids:
            result.append('\t\t%s = %s' % (g, g))

        result = '\n'.join(result)
        return result

    def make_projects(source_dir, projects):
        """Return a string representing all the projects in the .sln file

    It works by iterating over the projects and populating
    the project_template. Note that some projects are actually just
    solution folders. It is the responsibility of the caller to pass
    proper project objects that has type, name, filename, guid and dependencies
    attributes.
    """
        title()
        result = ''
        t1 = string.Template(project_template_with_dependencies)
        t2 = string.Template(project_template_without_dependencies)
        for p in projects:
            if p.type == project_type:
                filename = p.path[len(source_dir) + 1:].replace('/', '\\')
            else:
                filename = p.name
            dependency_guids = [get_guid(p.path) for d in p.dependencies]
            guid = get_guid(filename) if p.guid is None else p.guid
            d = dict(TypeGUID=p.type,
                     Name=p.name,
                     Filename=filename,
                     GUID=guid,
                     ProjectDependencies=make_project_dependencies(
                         p.dependencies))
            t = t1 if p.dependencies != [] else t2
            s = t.substitute(d)
            result += s

        return result[:-1]

    def make_configurations(projects):
        """Return a string representing all the project configuration platforms
       in the .sln file

    It works by iterating over the projects and populating
    the project_configuration_platform template.
    """
        title()
        result = ''
        t = string.Template(project_configuration_platform_template)
        for p in projects:
            d = dict(GUID=p.guid)
            s = t.substitute(d)
            result += s

        return result[:-1]

    def make_nested_projects(folders):
        """Return a string representing all the nested projects
       in the .sln file

    It works by iterating over the folders and populating
    the nested_project template with the guids of each nested project
    and its containing folder.

    Each folder is an object that has guid attribute and a projects
    attribute (which is a list of its contained projects)
    """
        title()
        for f in folders.values():
            assert hasattr(f, 'guid') and type(f.guid) == str
            assert hasattr(f, 'projects') and type(f.projects) in (list, tuple)

        result = ''
        nested_project = '\t\t${GUID} = ${FolderGUID}\n'
        t = string.Template(nested_project)
        for folder in folders.values():
            for p in folder.projects:
                d = dict(GUID=p.guid, FolderGUID=folder.guid)
                s = t.substitute(d)
                result += s

        return result[:-1]

    # Folders
    title()
    try:
        sln_filename = glob.glob(os.path.join(source_dir, '*.sln'))[0]
        existing_folders = get_existing_folders(sln_filename)
    except:
        existing_folders = []

    # Use folders GUIDs from existing .sln file (if there is any)
    for name, f in folders.items():
        if name in existing_folders:
            f.guid = existing_folders[name]
        else:
            f.guid = make_guid()

    # Prepare a flat list of all projects
    all_projects = []
    for f in folders.values():
        all_projects.append(f)
        all_projects += f.projects

    pp([p.name for p in all_projects])

    # Prepare the substitution dict for the solution template
    projects = [p for p in all_projects if p.type == project_type]
    all_projects = make_projects(source_dir, all_projects)

    configurations = make_configurations(projects)
    nested_projects = make_nested_projects(folders)
    d = dict(Projects=all_projects,
             Configurations=configurations,
             NestedProjects=nested_projects)

    # Create the final solution text by substituting the dict into the template
    t = string.Template(solution_template)
    solution = t.substitute(d)

    return solution