/// <summary> /// Generates and saves the code for the passed Gum ElementSave, but does not add the resulting .cs file to the VisualStudio project. /// </summary> /// <param name="element">The element to generate.</param> /// <returns></returns> public bool GenerateCodeFor(Gum.DataTypes.ElementSave element, CodeGenerationSavingBehavior savingBehavior = CodeGenerationSavingBehavior.AlwaysSave) { if (element == null) { throw new ArgumentNullException(nameof(element)); } bool wasSaved = false; string directoryToSave = GumRuntimesFolder; string generatedCode = mGueDerivingClassCodeGenerator.GenerateCodeFor(element); bool shouldSave; string saveLocation = directoryToSave + element.Name + "Runtime.Generated.cs"; if (savingBehavior == CodeGenerationSavingBehavior.AlwaysSave) { shouldSave = true; } else // if(savingBehavior == CodeGenerationSavingBehavior.SaveIfGeneratedDiffers) { // We only want to save this file if what we've just generated is different than what is already on disk: if (!System.IO.File.Exists(saveLocation)) { shouldSave = true; } else { var existingText = File.ReadAllText(saveLocation); shouldSave = existingText != generatedCode; } } if (!string.IsNullOrEmpty(generatedCode) && shouldSave) { wasSaved = TrySaveMultipleTimes(saveLocation, generatedCode); } return(wasSaved); }
/// <summary> /// Generates and saves the code for the passed Gum ElementSave (both generated and custom code template), /// but does not add the resulting .cs files to the VisualStudio project. /// </summary> /// <param name="element">The element to generate.</param> /// <returns>Information about what was generated and saved.</returns> public GenerationResult GenerateCodeFor(Gum.DataTypes.ElementSave element, CodeGenerationSavingBehavior savingBehavior = CodeGenerationSavingBehavior.AlwaysSave) { GenerationResult resultToReturn = new GenerationResult(); if (element == null) { throw new ArgumentNullException(nameof(element)); } string directoryToSave = GumRuntimesFolder; string generatedCode = mGueDerivingClassCodeGenerator.GenerateCodeFor(element); FilePath generatedSaveLocation = directoryToSave + element.Name + "Runtime.Generated.cs"; if (savingBehavior == CodeGenerationSavingBehavior.AlwaysSave) { resultToReturn.DidSaveGenerated = true; } else // if(savingBehavior == CodeGenerationSavingBehavior.SaveIfGeneratedDiffers) { // We only want to save this file if what we've just generated is different than what is already on disk: if (!generatedSaveLocation.Exists()) { resultToReturn.DidSaveGenerated = true; } else { var existingText = File.ReadAllText(generatedSaveLocation.FullPath); resultToReturn.DidSaveGenerated = existingText != generatedCode; } } string customCodeSaveLocation = directoryToSave + element.Name + "Runtime.cs"; // If it doesn't exist, overwrite it. If it does exist, don't overwrite it - we might lose // custom code. if (!System.IO.File.Exists(customCodeSaveLocation) && // Standard elements don't have CustomInit (element is StandardElementSave) == false) { resultToReturn.DidSaveCustom = true; } if (string.IsNullOrEmpty(generatedCode)) { resultToReturn.DidSaveCustom = false; resultToReturn.DidSaveGenerated = false; } if (resultToReturn.DidSaveGenerated) { // in case directory doesn't exist var directory = generatedSaveLocation.GetDirectoryContainingThis(); System.IO.Directory.CreateDirectory(directory.FullPath); GlueCommands.Self.TryMultipleTimes(() => System.IO.File.WriteAllText(generatedSaveLocation.FullPath, generatedCode)); } if (resultToReturn.DidSaveCustom) { var customCode = CustomCodeGenerator.Self.GetCustomCodeTemplateCode(element); GlueCommands.Self.TryMultipleTimes(() => System.IO.File.WriteAllText(customCodeSaveLocation, customCode)); } return(resultToReturn); }