All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.ide.visualstudio.tasks.internal.VisualStudioSolutionFile.groovy Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2013 the original author or authors.
 *
 * 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.
 */

package org.gradle.ide.visualstudio.tasks.internal

import com.google.common.collect.Maps
import com.google.common.collect.Sets
import org.gradle.api.Action
import org.gradle.ide.visualstudio.TextProvider
import org.gradle.ide.visualstudio.internal.VisualStudioProjectMetadata
import org.gradle.plugins.ide.internal.generator.AbstractPersistableConfigurationObject
import org.gradle.util.TextUtil

import static org.gradle.ide.visualstudio.internal.DefaultVisualStudioProject.getUUID

class VisualStudioSolutionFile extends AbstractPersistableConfigurationObject {
    List> actions = new ArrayList>();
    private Map projects = Maps.newLinkedHashMap()
    private Map> projectConfigurations = Maps.newLinkedHashMap()
    private baseText

    protected String getDefaultResourceName() {
        'default.sln'
    }

    void setProjects(List projects) {
        projects.each { VisualStudioProjectMetadata project ->
            this.projects[project.file] = project.name
            project.configurations.each { String configuration ->
                if (!this.projectConfigurations.containsKey(project.file)) {
                    this.projectConfigurations[project.file] = Sets.newHashSet();
                }
                this.projectConfigurations[project.file].add(configuration)
            }
        }
    }

    @Override
    void load(InputStream inputStream) throws Exception {
        baseText = inputStream.text
    }

    @Override
    void store(OutputStream outputStream) {
        def provider = new SimpleTextProvider()
        generateContent(provider.asBuilder())
        actions.each {
            it.execute(provider)
        }
        outputStream << TextUtil.convertLineSeparators(provider.getText(), TextUtil.getWindowsLineSeparator())
    }

    private void generateContent(StringBuilder builder) {
        builder << baseText
        projects.each { File projectFile, String projectName ->
            builder << """
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "${projectName}", "${projectFile.absolutePath}", "${getUUID(projectFile)}"
EndProject"""
        }
        builder << """
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution"""
        Set configurationNames = Sets.newLinkedHashSet(projectConfigurations.values().flatten().sort())
        configurationNames.each { String configurationName ->
            builder << """\n\t\t${configurationName}=${configurationName}"""
        }
        builder << """
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution"""
        projects.each { File projectFile, String projectName ->
            projectConfigurations[projectFile].sort().each { String configurationName ->
                builder << """\n\t\t${getUUID(projectFile)}.${configurationName}.ActiveCfg = ${configurationName}"""
                builder << """\n\t\t${getUUID(projectFile)}.${configurationName}.Build.0 = ${configurationName}"""
            }
        }


        builder << """
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal
"""
    }

    static class SimpleTextProvider implements TextProvider {
        private final StringBuilder builder = new StringBuilder();
        StringBuilder asBuilder() {
            return builder
        }

        String getText() {
            return builder.toString()
        }

        void setText(String value) {
            builder.replace(0, builder.length(), value)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy