protected GadgeteerModel LoadGadgeteerModel(string fileName) { string tempFileName = null; Transaction loadTransaction = null; try { tempFileName = CreateTempFileIfDirty(fileName); Store store = new Store(GlobalServiceProvider, typeof(GadgeteerDSLDomainModel)); loadTransaction = store.TransactionManager.BeginTransaction("Load", true); GadgeteerModel model = GadgeteerDSLSerializationHelper.Instance.LoadModel(store, tempFileName ?? fileName, null, null, null); loadTransaction.Commit(); return(model); } finally { if (tempFileName != null) { File.Delete(tempFileName); } if (loadTransaction != null) { loadTransaction.Dispose(); } } }
private void GenerateModulesDeclaration(CodeTypeDeclaration programClass, GadgeteerModel model) { foreach (Module module in model.Modules) { CodeMemberField memberField = new CodeMemberField(module.ModuleType, module.Name); memberField.UserData["WithEvents"] = true; string connectionsText = "(not connected)"; if (module.SocketUses.Count < 1) { connectionsText = "(no sockets)"; } else if (module.Connected && module.SocketUses.Any(u => u.Socket != null)) { var connections = from use in module.SocketUses where use.Socket != null group use by use.Socket.GadgeteerHardware into byHardware select new { Hardware = byHardware.Key, Sockets = byHardware.Select(u => u.Socket), Count = byHardware.Count() }; connectionsText = "using " + string.Join(" and ", from connection in connections select string.Format("socket{0} {1}{2} of {3}", connection.Count > 1 ? "s" : "", connection.Count > 1 ? string.Join(", ", connection.Sockets.Select(s => s.Label).Take(connection.Count - 1)) + " and " : "", connection.Sockets.Last().Label, connection.Hardware is Module ? ((Module)connection.Hardware).Name : "the mainboard")); } memberField.Comments.Add(new CodeCommentStatement(string.Format("<summary>The {0} module {1}.</summary>", module.GadgeteerPartDefinition.Name, connectionsText), true)); programClass.Members.Add(memberField); } }
/// <summary> /// Generates and returns the generated output /// </summary> /// <param name="inputFileName">The name of the file the custom tool is being run against</param> /// <param name="inputFileContent">The contents of the file the custom tool is being run against</param> protected override byte[] GenerateCode(string inputFileName, string inputFileContent) { GadgeteerModel model = LoadGadgeteerModel(inputFileName); if (model == null) { OnError("Unable to load model: {0}", inputFileName); return(null); } string userCodeFileName = Path.ChangeExtension(inputFileName, CodeProvider.FileExtension); model.GenerateUsingsInUserCode(GlobalServiceProvider, userCodeFileName); CodeCompileUnit code = new CodeCompileUnit(); CodeTypeDeclaration programClass; CodeStatementCollection mainStatements; CodeStatementCollection initializeModuleStatements; CodeMemberProperty mainboard; GenerateFileHeader(inputFileName, code, out programClass, out mainStatements, out initializeModuleStatements, out mainboard); GenerateMainMethod(mainStatements, model); GenerateModulesDeclaration(programClass, model); GenerateModulesInitialization(initializeModuleStatements, model); GenerateMainboardProperty(mainboard, model); if (model.Store != null) { model.Store.Dispose(); } using (MemoryStream codeStream = new MemoryStream()) { IndentedTextWriter codeWriter = new IndentedTextWriter(new StreamWriter(codeStream)); try { CodeProvider.GenerateCodeFromCompileUnit(code, codeWriter, null); } catch (Exception compileException) { OnError("Code generation failed: {0}", compileException.Message); return(new byte[0]); } codeWriter.Flush(); return(codeStream.ToArray()); } }
/// <summary> /// Add a reference to the assembly when a module/mainboard gets added /// </summary> private void GadgeteerHardwareAdded(object sender, ElementAddedEventArgs e) { var hw = e.ModelElement as GadgeteerHardware; if (hw == null) { return; } AddAssemblyReferences(hw); var mb = hw as Mainboard; if (mb != null) { GadgeteerModel.StoreLastUsedMainboardDefinition(mb.GadgeteerPartDefinition as MainboardDefinition, ProjectMFVersion); } }
private void GenerateMainboardProperty(CodeMemberProperty mainboard, GadgeteerModel model) { CodePropertyReferenceExpression baseMainboardReference = new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("Gadgeteer.Program"), "Mainboard"); CodeTypeReference mainboardTypeReference = new CodeTypeReference("Gadgeteer.Mainboard"); if (model.Mainboard != null) { mainboardTypeReference = new CodeTypeReference(model.Mainboard.MainboardDefinitionTypeName); } mainboard.GetStatements.Add( new CodeMethodReturnStatement( new CodeCastExpression(mainboardTypeReference, baseMainboardReference))); mainboard.SetStatements.Add( new CodeAssignStatement( baseMainboardReference, new CodeVariableReferenceExpression("value"))); mainboard.Type = mainboardTypeReference; }
private void GenerateMainMethod(CodeStatementCollection mainStatements, GadgeteerModel model) { mainStatements.Add(new CodeCommentStatement("Important to initialize the Mainboard first")); if (model.Mainboard == null) { GenerateIssueStatements(new GadgeteerModel.Issue(GadgeteerModel.IssueLevel.Error, "No mainboard is defined. Please add a mainboard in the Gadgeteer Designer."), mainStatements); } else { CodePropertyReferenceExpression mainboardReference = new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("Program"), "Mainboard"); mainStatements.Add(new CodeAssignStatement( mainboardReference, new CodeObjectCreateExpression(model.Mainboard.MainboardDefinitionTypeName))); } mainStatements.Add(new CodeVariableDeclarationStatement("Program", "p", new CodeObjectCreateExpression("Program"))); mainStatements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("p"), "InitializeModules")); mainStatements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("p"), "ProgramStarted")); mainStatements.Add(new CodeCommentStatement("Starts Dispatcher")); mainStatements.Add(new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("p"), "Run")); }
private void RemoveUsingStatements(GadgeteerHardware hw) { var ns = GadgeteerModel.GetNamespace(hw.GadgeteerPartDefinition.Type); var elements = this.Store.ElementDirectory.FindElements(GadgeteerHardware.DomainClassId); // check for other elements that have the same namespace before removing the using statement foreach (var element in elements) { if (GadgeteerModel.GetNamespace(((GadgeteerHardware)element).GadgeteerPartDefinition.Type) == ns) { return; } } var vsp = (VSProject2)project.Object; var userCodeItem = vsp.DTE.Solution.FindProjectItem(this.GetCodeFileName()); if (userCodeItem == null) { return; } var fcm = (FileCodeModel2)userCodeItem.FileCodeModel; foreach (CodeElement codeElement in fcm.CodeElements) { if (codeElement.Kind == vsCMElement.vsCMElementImportStmt) { var s = ((CodeImport)codeElement).Namespace; if (s == ns) { fcm.Remove(codeElement); } } } }
private void GenerateModulesInitialization(CodeStatementCollection initializeModuleStatements, GadgeteerModel model) { List <string> instantiatedModules = new List <string>(); IEnumerable <GadgeteerModel.Issue> issues; var modules = model.SortModulesInCodeGenerationOrder(out issues); foreach (var issue in issues) { GenerateIssueStatements(issue, initializeModuleStatements); } foreach (Module module in modules) { if (module.Connected) { string missingName = module.FindFirstMissingModuleName(instantiatedModules); if (missingName == null) { CodeFieldReferenceExpression moduleReference = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), module.Name); CodeAssignStatement statement = new CodeAssignStatement( moduleReference, new CodeObjectCreateExpression(module.AliasedTypeName, module.GenerateConstructorParameters())); initializeModuleStatements.Add(statement); instantiatedModules.Add(module.Name); } else { GenerateIssueStatements(new GadgeteerModel.Issue(GadgeteerModel.IssueLevel.Warning, "The module '{0}' requires the module '{1}' to be connected, so it will be null.", module.Name, missingName), initializeModuleStatements); } } else { //Socket dependentSocket = module.Sockets.FirstOrDefault(s => s.SocketUse != null && s.SocketUse.Module != null); //if (dependentSocket != null) // GenerateIssueStatements(new GadgeteerModel.Issue(EventLevel.Error, "The {0} was not connected in the designer, and is required by {1}.", module.Name, dependentSocket.SocketUse.Module.Name), initializeModuleStatements); //else GenerateIssueStatements(new GadgeteerModel.Issue(GadgeteerModel.IssueLevel.Warning, "The module '{0}' was not connected in the designer and will be null.", module.Name), initializeModuleStatements); } } }