Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add flag to prevent SFG regen on save #97

Open
jschneider1207 opened this issue May 12, 2017 · 2 comments
Open

Add flag to prevent SFG regen on save #97

jschneider1207 opened this issue May 12, 2017 · 2 comments

Comments

@jschneider1207
Copy link

jschneider1207 commented May 12, 2017

Is there a way to not have the Scripty Custom Tool run scripts when they are saved? I only want my scripts executed when I explicitly right click and hit "Run Custom Tool" because they take a very long time to run.

@StingyJack
Copy link
Contributor

Yes. I have the way I do that with T4 templates that works in Scripty as well.

#63 has some deets. but its essentially only going to run when a specific build configuration is present.

I put this near the top of the csx file


if (ShouldRegenOccur(Context)== false)
{
    WriteExistingOutputFileBackToItself(Context)
    return;
}

This is a ScriptyHelpers.csx file I ref that has the above functions in it

using System.Linq;

#region "regeneration helpers"

public const string BUILD_CONFIG_NEEDED_TO_RUN_CODE_GEN = "RegenCodeFiles";

public bool ShouldRegenOccur(ScriptContext context)
{
    try
    {
        var activeProjectConfiguration = GetActiveProjectConfiguration(context);
        return BUILD_CONFIG_NEEDED_TO_RUN_CODE_GEN.Equals(activeProjectConfiguration, StringComparison.OrdinalIgnoreCase);
    }
    catch (System.Exception)
    {
        //ignoring for now
    }
    return false;
}

public string GetActiveProjectConfiguration(ScriptContext context)
{
    var projectCollection = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection;
    var thisProj = projectCollection.LoadProject(context.Project.FilePath);
    var currentSlnCfg = thisProj.GlobalProperties["CurrentSolutionConfigurationContents"];
    var projectConfigs = System.Xml.Linq.XDocument.Parse(currentSlnCfg);
    var thisProjectNode = projectConfigs.Root.Descendants("ProjectConfiguration").FirstOrDefault(p => p.Attribute("AbsolutePath").Value == Project.FilePath);
    var projBuildValue = thisProjectNode.Value;
    var splits = projBuildValue.Split('|');
    var win = splits[0];
    return win;
}

public void WriteExistingOutputFileBackToItself(ScriptContext context)
{
    var fileName = System.IO.Path.ChangeExtension(context.ScriptFilePath, ".cs");
    context.Output.Write(System.IO.File.ReadAllText(fileName));
}

public string GetGenerationHeader()
{
    return $"// Generated at {DateTime.UtcNow} UTC by {Environment.UserName}";
}

#endregion //#region "regeneration helpers"


Also, I dont remember if the PR made it that had a configuration panel in VS for scripty, but you could get it, add a flag in Tools --> options --> scripty for "when to execute" and have the IVSSingleFileGenerator look there first.

@daveaglick
Copy link
Owner

I wonder if it would be beneficial to make an even simpler built-in flag for regenerate on save. Just set the flag to false and we'll bail out of the single file generator before anything gets overwritten.

@daveaglick daveaglick changed the title Don't execute scripts on save Add flag to prevent SFG regen on save Jul 4, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants