Ejemplo n.º 1
0
        //public static IEnumerable<CodeFileGenerator> GetAllFileGenerators(GeneratorSettings settings)
        //{
        //    return GetAllFileGenerators(settings, CurrentProject);
        //}
        /// <summary>
        /// Get all of the merged generators for a project, this will merge any output generators with the same filename into a combined "CodeFileGenerator".
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <CodeFileGenerator> GetAllFileGenerators(IGraphConfiguration config, IDataRecord[] items, bool includeDisabled = false)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            var codeGenerators = GetAllCodeGenerators(config, items, includeDisabled).ToArray();
            var groups         = codeGenerators.GroupBy(p => Path.Combine(config.CodeOutputPath, p.Filename)).Distinct();

            foreach (var @group in groups)
            {
                var generator = new CodeFileGenerator(config.Namespace)
                {
                    AssetPath = @group.Key.Replace("\\", "/"),
#if UNITY_EDITOR
                    //SystemPath = Path.Combine(config.CodeOutputSystemPath, @group.Key.Substring(7)).Replace("\\", "/"),
                    SystemPath = @group.Key,
#endif
                    Generators = @group.ToArray()
                };
                yield return(generator);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// This method gets every method, property, or constructor generated for a node, pass in an item filter to filter only to specific items
 /// that are set up as iterators on templates.
 ///
 /// For Example:
 /// You can get only the members of a class that have been added since last save and compile by comparing with with the change tracking data.
 /// </summary>
 /// <param name="node"></param>
 /// <param name="itemFilter"></param>
 /// <returns></returns>
 public static IEnumerable <TemplateMemberResult> GetEditableOutputMembers(this IDiagramNodeItem node, IGraphConfiguration config, Predicate <IDiagramNodeItem> itemFilter = null)
 {
     foreach (var item in GetAllEditableFilesForNode(node, config).OfType <ITemplateClassGenerator>())
     {
         var gen = new CodeFileGenerator()
         {
             Generators = new[] { item as OutputGenerator },
             SystemPath = string.Empty
         };
         gen.Namespace = new CodeNamespace();
         gen.Unit      = new CodeCompileUnit();
         gen.Unit.Namespaces.Add(gen.Namespace);
         item.ItemFilter = itemFilter;
         item.Initialize(gen);
         foreach (var result in item.Results)
         {
             yield return(result);
         }
     }
 }
Ejemplo n.º 3
0
        private static void GenerateFile(FileInfo fileInfo, CodeFileGenerator codeFileGenerator)
        {
            // Get the path to the directory
            var directory = System.IO.Path.GetDirectoryName(fileInfo.FullName);

            // Create it if it doesn't exist
            if (directory != null && !Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            try
            {
                // Write the file
                File.WriteAllText(fileInfo.FullName, codeFileGenerator.ToString());
            }
            catch (Exception ex)
            {
                InvertApplication.LogException(ex);
                InvertApplication.Log("Coudln't create file " + fileInfo.FullName);
            }
        }
Ejemplo n.º 4
0
 public void FileSkipped(CodeFileGenerator codeFileGenerator)
 {
 }
Ejemplo n.º 5
0
 public void FileGenerated(CodeFileGenerator generator)
 {
 }
Ejemplo n.º 6
0
        public IEnumerator Generate(SaveAndCompileCommand command)
        {
            var repository = InvertGraphEditor.Container.Resolve <IRepository>();

            var remove = repository.AllOf <IClassNode>().Where(p => string.IsNullOrEmpty(p.Name)).ToArray();

            foreach (var item in remove)
            {
                repository.Remove(item);
            }
            //var allIds = allRecords.Select(p => p.Identifier).ToArray();

            //foreach (var item in allRecords)
            //{
            //    if (item.ForeignKeys.Where(p=>!string.IsNullOrEmpty(p)).Any(p =>!allIds.Contains(p)))
            //    {
            //        InvertApplication.Log(string.Format("{0} : {1} was removed due to invalid foreign key.", item.Identifier, item.GetType().Name));
            //        repository.Remove(item);
            //    }
            //}
            repository.Commit();
            var config = InvertGraphEditor.Container.Resolve <IGraphConfiguration>();
            var items  = GetItems(repository, command.ForceCompileAll).Distinct().ToArray();

            yield return
                (new TaskProgress(0f, "Validating"));

            var a = ValidationSystem.ValidateNodes(items.OfType <IDiagramNode>().ToArray());

            while (a.MoveNext())
            {
                yield return(a.Current);
            }
            if (ValidationSystem.ErrorNodes.SelectMany(n => n.Errors).Any(e => e.Siverity == ValidatorType.Error))
            {
                Signal <INotify>(_ => _.Notify("Please, fix all errors before compiling.", NotificationIcon.Error));
                yield break;
            }
            Signal <IUpgradeDatabase>(_ => _.UpgradeDatabase(config as uFrameDatabaseConfig));
            Signal <ICompilingStarted>(_ => _.CompilingStarted(repository));
            // Grab all the file generators
            var fileGenerators = InvertGraphEditor.GetAllFileGenerators(config, items, true).ToArray();

            var length = 100f / (fileGenerators.Length + 1);
            var index  = 0;

            foreach (var codeFileGenerator in fileGenerators)
            {
                index++;
                yield return(new TaskProgress(length * index, "Generating " + System.IO.Path.GetFileName(codeFileGenerator.AssetPath)));

                // Grab the information for the file
                var fileInfo = new FileInfo(codeFileGenerator.SystemPath);
                // Make sure we are allowed to generate the file
                if (!codeFileGenerator.CanGenerate(fileInfo))
                {
                    var fileGenerator = codeFileGenerator;
                    InvertApplication.SignalEvent <ICompileEvents>(_ => _.FileSkipped(fileGenerator));

                    if (codeFileGenerator.Generators.All(p => p.AlwaysRegenerate))
                    {
                        if (File.Exists(fileInfo.FullName))
                        {
                            File.Delete(fileInfo.FullName);
                        }
                    }

                    continue;
                }

                GenerateFile(fileInfo, codeFileGenerator);
                CodeFileGenerator generator = codeFileGenerator;
                InvertApplication.SignalEvent <ICompileEvents>(_ => _.FileGenerated(generator));
            }
            ChangedRecrods.Clear();
            InvertApplication.SignalEvent <ICompileEvents>(_ => _.PostCompile(config, items));
            foreach (var item in items.OfType <IGraphData>())
            {
                item.IsDirty = false;
            }
            yield return
                (new TaskProgress(100f, "Complete"));

#if UNITY_EDITOR
            repository.Commit();
            if (InvertGraphEditor.Platform != null) // Testability
            {
                InvertGraphEditor.Platform.RefreshAssets();
            }
#endif
        }
Ejemplo n.º 7
0
 public override void Initialize(CodeFileGenerator codeFileGenerator)
 {
     base.Initialize(codeFileGenerator);
     TryAddNamespace("System.IO");
 }
        public void FileSkipped(CodeFileGenerator codeFileGenerator)
        {

        }
        public void FileGenerated(CodeFileGenerator generator)
        {

        }
    public override void Initialize(CodeFileGenerator fileGenerator)
    {
        base.Initialize(fileGenerator);

        //Throw in NUnit
        base.Namespace.Imports.Add(new CodeNamespaceImport("NUnit.Framework"));

        //Declare test suite class
        var testSuitDeclaration = new CodeTypeDeclaration
        {
            Name = _testSuiteData.Name + "TestSuite",
            IsClass = true,
        };

        //Adding text fixture attribute
        testSuitDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference("TestFixture")));

        foreach (var scenario in _testSuiteData.Scenarios)
        {
            var isMultiinstance = _elementData.IsMultiInstance;
            var useParameter = !String.IsNullOrEmpty(scenario.CommandData.RelatedTypeName);
            const string controllerVariableName = "controller";
            const string argumentVariableName = "argument";
            const string senderVariableName = "sender";

            //Setup scenario method
            var testMethod = new CodeMemberMethod
            {
                Name = scenario.Name.Replace(" ", "_"),
                Attributes = MemberAttributes.Public,
            };
            testMethod.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference("Test")));

            //Create controller instance + comment
            var controllerDeclaractionComment = new CodeCommentStatement("GIVEN: setup your environment here");
            var contollerDeclaractionStatement = new CodeVariableDeclarationStatement(
                new CodeTypeReference(_elementData.NameAsController),
                controllerVariableName,
                new CodeObjectCreateExpression(_elementData.NameAsController));

            testMethod.Statements.Add(new CodeSnippetStatement(""));
            testMethod.Statements.Add(controllerDeclaractionComment);
            testMethod.Statements.Add(contollerDeclaractionStatement);

            //Create viewmodel if multiinstance
            if (isMultiinstance)
            {
                var viewModelDeclaration = new CodeVariableDeclarationStatement(
                    new CodeTypeReference(_elementData.NameAsViewModel),
                    senderVariableName,
                    new CodeObjectCreateExpression(
                        new CodeTypeReference(_elementData.NameAsViewModel),
                        new CodeSnippetExpression(controllerVariableName)));

                testMethod.Statements.Add(viewModelDeclaration);
            }

            //Create parameter if needed
            if (useParameter)
            {
                CodeVariableDeclarationStatement argumentDeclaration = null;
                var viewModel = _elementData.OwnerData.GetViewModel(scenario.CommandData.RelatedTypeName);

                if (viewModel == null)
                    argumentDeclaration = new CodeVariableDeclarationStatement(
                        new CodeTypeReference(scenario.CommandData.RelatedTypeName),
                        argumentVariableName,
                        new CodeDefaultValueExpression(new CodeTypeReference(scenario.CommandData.RelatedTypeName))
                        );
                else
                    argumentDeclaration = new CodeVariableDeclarationStatement(
                        new CodeTypeReference(viewModel.NameAsViewModel), argumentVariableName, new CodeDefaultValueExpression(new CodeTypeReference(viewModel.NameAsViewModel)));
                testMethod.Statements.Add(argumentDeclaration);
            }

            //Create invocation to the controller + comment
            var commandInvocationComment = new CodeCommentStatement("WHEN: call to the command");
            var commandInvocation = new CodeMethodInvokeExpression(
                new CodeSnippetExpression(controllerVariableName),
                scenario.CommandData.Name);

            if(isMultiinstance)
            commandInvocation.Parameters.Add(new CodeSnippetExpression(senderVariableName));
            if(useParameter)
            commandInvocation.Parameters.Add(new CodeSnippetExpression(argumentVariableName));

            testMethod.Statements.Add(new CodeSnippetStatement(""));
            testMethod.Statements.Add(commandInvocationComment);
            testMethod.Statements.Add(commandInvocation);

            //Create template assertion + comment
            var assertionComment = new CodeCommentStatement("THEN: Assert anything here");
            var assertionInvocation = new CodeSnippetExpression("Assert.That(true)");

            testMethod.Statements.Add(new CodeSnippetStatement(""));
            testMethod.Statements.Add(assertionComment);
            testMethod.Statements.Add(assertionInvocation);

            testSuitDeclaration.Members.Add(testMethod);
        }
        base.Namespace.Types.Add(testSuitDeclaration);
    }