/// <summary> /// Preloads data of a macro from a file. Returns the type name of the loaded macro on success. /// </summary> /// <param name="fileName">the name of the file</param> /// <returns>the typename of the macro if loading was successful</returns> public string LoadMacro(string fileName) { fileName = Application.StartupPath + @"\Macros\" + Path.GetFileName(fileName); if (File.Exists(fileName) == false) { return(String.Empty); } MacroData macroData = MacroSerializer.DeserializeMacro(fileName); if (m_Data.ContainsKey(macroData.Name)) { m_Data.Remove(macroData.Name); } if (m_FileNames.ContainsKey(macroData.Name)) { m_FileNames.Remove(macroData.Name); } m_FileNames.Add(macroData.Name, fileName); m_Data.Add(macroData.Name, macroData); if (m_Macros.ContainsKey(macroData.Name)) { //if it's already present, replace it with the new one m_Macros.Remove(macroData.Name); m_Symbols.Remove(macroData.Name); } //Macro macro = new Macro(CircuitConverter.Instance.ConvertToCircuit(macroData.Circuit), macroData.Matching); //macro.FileReference = Path.GetFileName(fileName); //macro.TypeName = macroData.Name; //m_Macros.Add(macroData.Name, macro); //m_Symbols.Add(macroData.Name, SymbolConverter.Instance.ConvertToSymbol(macroData.Symbol)); return(macroData.Name); }
internal File PerformSave(MacroEditorModel model) { Mandate.ParameterNotNull(model, "model"); using (var uow = Hive.Create()) { var fileName = model.Alias.ToUmbracoAlias() + ".macro"; var newFileId = new HiveId(fileName); var macroFile = new File { Name = fileName, ContentBytes = Encoding.UTF8.GetBytes(MacroSerializer.ToXml(model).ToString()) }; var existing = uow.Repositories.Get <File>(newFileId); //delete the file first before re-saving as AddOrUpdate seems to append to the file if (existing != null) { uow.Repositories.Delete <File>(newFileId); } uow.Repositories.AddOrUpdate(macroFile); //TODO: the Hive IO provider commit seems to do nothing :( ... needs to be implemented! uow.Complete(); return(macroFile); } }
// Macro Share click private void Button_Click_38(object sender, RoutedEventArgs e) { Macro m = (Macro)((Button)sender).DataContext; string s = MacroSerializer.Save(m); Clipboard.SetText(s); }
public override void Deserialize(BinaryReader br) { this.Params[0] = MacroSerializer.ReadMacroAction(br); this.Params[1] = MacroSerializer.ReadMacroAction(br); this.Params[2] = MacroSerializer.ReadMacroAction(br); this.Params[3] = MacroSerializer.ReadMacroAction(br); this.Params[4] = MacroSerializer.ReadMacroAction(br); }
public override void Serialize(BinaryWriter bw) { MacroSerializer.WriteMacroAction(bw, this.Params[0]); MacroSerializer.WriteMacroAction(bw, this.Params[1]); MacroSerializer.WriteMacroAction(bw, this.Params[2]); MacroSerializer.WriteMacroAction(bw, this.Params[3]); MacroSerializer.WriteMacroAction(bw, this.Params[4]); }
// Macro Paste click private void Button_Click_40(object sender, RoutedEventArgs e) { try { Macro m = MacroSerializer.Load(Clipboard.GetText()); MacroSerializer.Macros.Add(m); Clipboard.SetText(string.Empty); } catch (Exception) { // NOOP } }
/// <summary> /// Gets a macro by alias. /// </summary> /// <param name="alias">The alias.</param> /// <returns></returns> private MacroEditorModel GetMacroByAlias(string alias) { using (var uow = BackOfficeRequestContext .Application .Hive.OpenReader <IFileStore>(new Uri("storage://macros"))) { var filename = alias + ".macro"; var macroFile = uow.Repositories.Get <File>(new HiveId(filename)); if (macroFile == null) { throw new ApplicationException("Could not find a macro with the specified alias: " + alias); } return(MacroSerializer.FromXml(Encoding.UTF8.GetString(macroFile.ContentBytes))); } }
private void SaveMacro(object sender, EventArgs e) { /* * FileSelector fileSelector = new FileSelector(FileSelectorFilters.Macros); * fileSelector.FileName = textBox_MacroName.Text; * if (fileSelector.ExecuteSaveDialog()) * { * MacroData macroData = new MacroData(textBox_MacroName.Text); * macroData.Circuit = m_CircuitData; * macroData.Symbol = m_SymbolData; * macroData.Matching = m_Matchings.ToArray(); * MacroSerializer.SerializeMacro(fileSelector.FileName, macroData); * } * /**/ string macroName = textBox_MacroName.Text; char[] invalid = Path.GetInvalidFileNameChars(); foreach (char inv in invalid) { int index = macroName.IndexOf(inv); while (index > -1) { macroName.Remove(index, 1); } } macroName.Replace(" ", "_"); string filename = Application.StartupPath + @"\Macros\" + macroName + @".xmac"; if (File.Exists(filename) && MessageBox.Show("Es existiert bereits ein Makro mit dem gewählten Namen. Soll dieses überschrieben werden?", "Warnung", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } MacroData macroData = new MacroData(textBox_MacroName.Text); macroData.Circuit = m_CircuitData; macroData.Symbol = m_SymbolData; macroData.Matching = m_Matchings.ToArray(); MacroSerializer.SerializeMacro(filename, macroData); MacroCache.Instance.LoadMacro(filename); Close(); }
public void MacroSerializer_FromXml() { var xml = @"<macro id=""my-macro.macro"" alias=""test"" name=""Test"" cacheByPage=""true"" cachePeriodSeconds=""1234"" cachePersonalized=""false"" macroType=""ChildAction"" renderContentInEditor=""true"" selectedItem=""RenderTwitterFeed"" useInEditor=""false"" />"; var macro = MacroSerializer.FromXml(xml); Assert.AreEqual("test", macro.Alias); Assert.AreEqual("Test", macro.Name); Assert.AreEqual(true, macro.CacheByPage); Assert.AreEqual(1234, macro.CachePeriodSeconds); Assert.AreEqual(false, macro.CachePersonalized); Assert.AreEqual("ChildAction", macro.MacroType); Assert.AreEqual(true, macro.RenderContentInEditor); Assert.AreEqual("RenderTwitterFeed", macro.SelectedItem); Assert.AreEqual(false, macro.UseInEditor); //TODO: Test parameters }
public override void Serialize(BinaryWriter bw) { bw.Write(this.If.Count); foreach (MacroAction ma in this.If) { MacroSerializer.WriteMacroAction(bw, ma); } bw.Write(this.Then.Count); foreach (MacroAction ma in this.Then) { MacroSerializer.WriteMacroAction(bw, ma); } bw.Write(this.Else.Count); foreach (MacroAction ma in this.Else) { MacroSerializer.WriteMacroAction(bw, ma); } }
public override ActionResult Edit(HiveId?id) { if (id.IsNullValueOrEmpty()) { return(HttpNotFound()); } using (var uow = Hive.Create()) { var macroFile = uow.Repositories.Get <File>(id.Value); if (macroFile != null) { var model = MacroSerializer.FromFile(macroFile); EnsureModelListData(model); return(View(model)); } } return(HttpNotFound()); }
public void MacroSerializer_ToXml() { var macro = new MacroEditorModel { Alias = "test", Name = "Test", Id = new HiveId("my-macro.macro"), CacheByPage = true, CachePeriodSeconds = 1234, CachePersonalized = false, MacroType = "ChildAction", RenderContentInEditor = true, SelectedItem = "RenderTwitterFeed", UseInEditor = false }; macro.MacroParameters.Add(new MacroParameterDefinitionModel { Alias = "test1", Name = "Test1", ParameterEditorId = Guid.NewGuid(), Show = true }); macro.MacroParameters.Add(new MacroParameterDefinitionModel { Alias = "test2", Name = "Test2", ParameterEditorId = Guid.NewGuid(), Show = false }); var xml = MacroSerializer.ToXml(macro); Assert.AreEqual(macro.Alias, xml.Root.Attribute("alias").Value); Assert.AreEqual(macro.Name, xml.Root.Attribute("name").Value); Assert.AreEqual(macro.CacheByPage, (bool)xml.Root.Attribute("cacheByPage")); Assert.AreEqual(macro.CachePeriodSeconds, (int)xml.Root.Attribute("cachePeriodSeconds")); Assert.AreEqual(macro.CachePersonalized, (bool)xml.Root.Attribute("cachePersonalized")); Assert.AreEqual(macro.MacroType.ToString(), xml.Root.Attribute("macroType").Value); Assert.AreEqual(macro.RenderContentInEditor, (bool)xml.Root.Attribute("renderContentInEditor")); Assert.AreEqual(macro.SelectedItem, xml.Root.Attribute("selectedItem").Value); Assert.AreEqual(macro.UseInEditor, (bool)xml.Root.Attribute("useInEditor")); //TODO: test parameter values Assert.AreEqual(2, xml.Root.Elements("parameter").Count()); }
public void Init() { Configuration = new uSyncCoreConfig(); ContentTypeSerializer = new ContentTypeSerializer(Constants.Packaging.DocumentTypeNodeName); MediaTypeSerializer = new MediaTypeSerializer("MediaType"); MemberTypeSerializer = new MemberTypeSerializer("MemberType"); TemplateSerializer = new TemplateSerializer(Constants.Packaging.TemplateNodeName); LanguageSerializer = new LanguageSerializer("Language"); DictionarySerializer = new DictionarySerializer(Constants.Packaging.DictionaryItemNodeName); MacroSerializer = new MacroSerializer(Constants.Packaging.MacroNodeName); DataTypeSerializer = new DataTypeSerializer(Constants.Packaging.DataTypeNodeName); ContentSerializer = new ContentSerializer(); MediaSerializer = new MediaSerializer(); MediaFileMover = new uSyncMediaFileMover(); }
public override void Deserialize(BinaryReader br) { int i = br.ReadInt32(); this.If.Clear(); while (i-- > 0) { this.If.AddLast(MacroSerializer.ReadMacroAction(br)); } i = br.ReadInt32(); this.Then.Clear(); while (i-- > 0) { this.Then.AddLast(MacroSerializer.ReadMacroAction(br)); } i = br.ReadInt32(); this.Else.Clear(); while (i-- > 0) { this.Else.AddLast(MacroSerializer.ReadMacroAction(br)); } }
public override void Deserialize(BinaryReader br) => this.Params[0] = MacroSerializer.ReadMacroAction(br);
public override void Serialize(BinaryWriter bw) => MacroSerializer.WriteMacroAction(bw, this.Params[0]);
public static CreatePackageResult CreatePackage(string id, PackageDefinition packageDef, IRebelApplicationContext appContext, HttpContextWrapper httpContext) { var result = new CreatePackageResult(); try { var packagePath = string.Format("~/App_Data/Rebel/CreatedPackages/{0}", id); var packageDir = new DirectoryInfo(httpContext.Server.MapPath(packagePath)); var packageFile = new FileInfo(Path.Combine(packageDir.FullName, "package.nupkg")); // Build package var builder = new PackageBuilder { Id = packageDef.Alias, Title = packageDef.Name, Version = new Version(packageDef.Version), Description = packageDef.Description, ProjectUrl = new Uri(packageDef.ProjectUrl) }; builder.Authors.Add(packageDef.Author); if (!string.IsNullOrWhiteSpace(packageDef.Tags)) { builder.Tags.AddRange(packageDef.Tags.Split(' ')); } if (!string.IsNullOrWhiteSpace(packageDef.LicenseUrl)) { builder.LicenseUrl = new Uri(packageDef.LicenseUrl); } using (var uow = appContext.Hive.OpenReader <IContentStore>()) { var uploadedFilesToPackage = new List <HiveId>(); // Content if (!packageDef.ContentNodeId.IsNullValueOrEmpty()) { var relationsToSerialize = new List <IRelationById>(); var nodesToSerialize = new List <TypedEntity>(); var contentNode = uow.Repositories.Get <TypedEntity>(packageDef.ContentNodeId); nodesToSerialize.Add(contentNode); var parentRelations = uow.Repositories.GetParentRelations(contentNode.Id, FixedRelationTypes.DefaultRelationType); relationsToSerialize.AddRange(parentRelations); if (packageDef.IncludeChildContentNodes) { var childrenRelations = uow.Repositories.GetDescendentRelations(contentNode.Id, FixedRelationTypes.DefaultRelationType); nodesToSerialize.AddRange(uow.Repositories.Get <TypedEntity>(true, childrenRelations.Select(x => x.DestinationId).ToArray())); relationsToSerialize.AddRange(childrenRelations); } foreach (var node in nodesToSerialize.Where(x => x.Attributes.Any(y => y.AttributeDefinition.AttributeType.RenderTypeProvider.InvariantEquals(CorePluginConstants.FileUploadPropertyEditorId)))) { var attributes = node.Attributes.Where(x => x.AttributeDefinition.AttributeType.RenderTypeProvider.InvariantEquals(CorePluginConstants.FileUploadPropertyEditorId)); uploadedFilesToPackage.AddRange(attributes.Where(x => x.Values["Value"] != null).Select(attribute => HiveId.Parse(attribute.Values["Value"].ToString()))); } CopySerializedObjectsToPackage(builder, appContext, nodesToSerialize, node => "Data/Content/" + node.Id.ToString().ToMd5() + ".json"); // Relations CopySerializedObjectsToPackage(builder, appContext, relationsToSerialize, relation => "Data/Content/Relations/" + relation.SourceId.ToString().ToMd5() + "-" + relation.DestinationId.ToString().ToMd5() + ".json"); } // Media if (!packageDef.MediaNodeId.IsNullValueOrEmpty()) { var relationsToSerialize = new List <IRelationById>(); var nodesToSerialize = new List <TypedEntity>(); var mediaNode = uow.Repositories.Get <TypedEntity>(packageDef.MediaNodeId); if (mediaNode != null) { nodesToSerialize.Add(mediaNode); var parentRelations = uow.Repositories.GetParentRelations(mediaNode.Id, FixedRelationTypes.DefaultRelationType); relationsToSerialize.AddRange(parentRelations); if (packageDef.IncludeChildContentNodes) { var childrenRelations = uow.Repositories.GetDescendentRelations(mediaNode.Id, FixedRelationTypes.DefaultRelationType); nodesToSerialize.AddRange(uow.Repositories.Get <TypedEntity>(true, childrenRelations.Select(x => x.DestinationId).ToArray())); relationsToSerialize.AddRange(childrenRelations); } } foreach (var node in nodesToSerialize.Where(x => x.Attributes.Any(y => y.AttributeDefinition.AttributeType.RenderTypeProvider.InvariantEquals(CorePluginConstants.FileUploadPropertyEditorId)))) { var attributes = node.Attributes.Where(x => x.AttributeDefinition.AttributeType.RenderTypeProvider.InvariantEquals(CorePluginConstants.FileUploadPropertyEditorId)); uploadedFilesToPackage.AddRange(attributes.Where(x => x.Values["Value"] != null).Select(attribute => HiveId.Parse(attribute.Values["Value"].ToString()))); } CopySerializedObjectsToPackage(builder, appContext, nodesToSerialize, node => "Data/Media/" + node.Id.ToString().ToMd5() + ".json"); // Relations CopySerializedObjectsToPackage(builder, appContext, relationsToSerialize, relation => "Data/Media/Relations/" + relation.SourceId.ToString().ToMd5() + "-" + relation.DestinationId.ToString().ToMd5() + ".json"); } // Files CopyFilesToPackage(builder, appContext, "storage://file-uploader", uploadedFilesToPackage, "Content/Content/Media/"); // Dictionary Items var dictionaryRelationsToSerialize = new List <IRelationById>(); var dictionaryItems = uow.Repositories.Get <TypedEntity>(true, packageDef.DictionaryItemIds.ToArray()); CopySerializedObjectsToPackage(builder, appContext, dictionaryItems, dictionaryItem => "Data/DictionaryItems/" + dictionaryItem.Id.ToString().ToMd5() + ".json"); foreach (var parentRelations in dictionaryItems.Select(dictionaryItem => uow.Repositories.GetParentRelations(dictionaryItem.Id))) { dictionaryRelationsToSerialize.AddRange(parentRelations); } CopySerializedObjectsToPackage(builder, appContext, dictionaryRelationsToSerialize, relation => "Data/DictionaryItems/Relations/" + relation.SourceId.ToString().ToMd5() + "-" + relation.DestinationId.ToString().ToMd5() + ".json"); // Doc Types var docTypeRelationsToSerialize = new List <IRelationById>(); var docTypes = uow.Repositories.Schemas.Get <EntitySchema>(true, packageDef.DocumentTypeIds.ToArray()); CopySerializedObjectsToPackage(builder, appContext, docTypes, docType => "Data/DocumentTypes/" + docType.Alias + ".json"); foreach (var parentRelations in docTypes.Select(docType => uow.Repositories.Schemas.GetParentRelations(docType.Id))) { docTypeRelationsToSerialize.AddRange(parentRelations); } CopySerializedObjectsToPackage(builder, appContext, docTypeRelationsToSerialize, relation => "Data/DocumentTypes/Relations/" + relation.SourceId.ToString().ToMd5() + "-" + relation.DestinationId.ToString().ToMd5() + ".json"); // Media Types var mediaTypeRelationsToSerialize = new List <IRelationById>(); var mediaTypes = uow.Repositories.Schemas.Get <EntitySchema>(true, packageDef.MediaTypeIds.ToArray()); CopySerializedObjectsToPackage(builder, appContext, mediaTypes, mediaType => "Data/MediaTypes/" + mediaType.Alias + ".json"); foreach (var parentRelations in mediaTypes.Select(mediaType => uow.Repositories.Schemas.GetParentRelations(mediaType.Id))) { mediaTypeRelationsToSerialize.AddRange(parentRelations); } CopySerializedObjectsToPackage(builder, appContext, mediaTypeRelationsToSerialize, relation => "Data/MediaTypes/Relations/" + relation.SourceId.ToString().ToMd5() + "-" + relation.DestinationId.ToString().ToMd5() + ".json"); // Data Types var dataTypes = uow.Repositories.Schemas.Get <AttributeType>(true, packageDef.DataTypeIds.ToArray()); CopySerializedObjectsToPackage(builder, appContext, dataTypes, dataType => "Data/DataTypes/" + dataType.Alias + ".json"); } // Templates CopyFilesToPackage(builder, appContext, "storage://templates", packageDef.TemplateIds, "Content/Views/"); // Partials CopyFilesToPackage(builder, appContext, "storage://partials", packageDef.PartialIds, "Content/Views/Partials/"); // Stylesheets CopyFilesToPackage(builder, appContext, "storage://stylesheets", packageDef.StylesheetIds, "Content/Content/Styles/"); // Scripts CopyFilesToPackage(builder, appContext, "storage://scripts", packageDef.ScriptIds, "Content/Scripts/"); // Macros CopyFilesToPackage(builder, appContext, "storage://macros", packageDef.MacroIds, "Content/App_Data/Rebel/Macros/", (file, packageBuilder) => { var macro = MacroSerializer.FromFile(file); if (macro.MacroType == "PartialView") { var macroParts = macro.SelectedItem.Split('-'); var areaName = macroParts.Length > 1 ? macroParts[0] : ""; var macroName = (areaName.IsNullOrWhiteSpace()) ? string.Join("", macroParts) : macroParts[1]; var relativePath = (areaName.IsNullOrWhiteSpace()) ? "~/Views/MacroPartials/" + macroName + ".cshtml" : "~/App_Plugins/Packages/" + areaName + "/Views/MacroPartials/" + macroName + ".cshtml"; var path = httpContext.Server.MapPath(relativePath); packageBuilder.Files.Add(new PhysicalPackageFile { SourcePath = path, TargetPath = "Content/Views/MacroPartials/" + macroName + ".cshtml" }); } }); // Languages var languages = appContext.Settings.Languages.Where(x => packageDef.LanguageIds.Contains(x.IsoCode)); CopySerializedObjectsToPackage(builder, appContext, languages, lang => "Data/Languages/" + lang.IsoCode + ".json"); // Misc files foreach (var file in packageDef.AdditionalFiles) { var cleanFile = "~/" + file.Replace('\\', '/').TrimStart('~', '/'); var cleanFilePath = httpContext.Server.MapPath(cleanFile); if (!File.Exists(cleanFilePath) && !Directory.Exists(cleanFilePath)) { continue; } var fileInfo = File.GetAttributes(cleanFilePath); var isDirectory = (fileInfo & FileAttributes.Directory) == FileAttributes.Directory; if (cleanFile.StartsWith("~/App_Plugins/Packages/" + packageDef.Alias + "/", true, CultureInfo.InvariantCulture)) { if (isDirectory) { CopyFolderToPackage(builder, appContext, httpContext.Server.MapPath("~/App_Plugins/Packages/" + packageDef.Alias + "/"), cleanFilePath, (rootPath, path) => path.TrimStart(rootPath).Replace('\\', '/')); } else { builder.Files.Add(new PhysicalPackageFile { SourcePath = httpContext.Server.MapPath(cleanFile), TargetPath = Regex.Replace(cleanFile, "^~/App_Plugins/Packages/" + packageDef.Alias + "/", "", RegexOptions.IgnoreCase) }); } } else if (cleanFile.StartsWith("~/Bin/", true, CultureInfo.InvariantCulture)) { if (isDirectory) { CopyFolderToPackage(builder, appContext, httpContext.Server.MapPath("~/Bin/"), cleanFilePath, (rootPath, path) => "lib/" + path.TrimStart(rootPath).Replace('\\', '/')); } else { builder.Files.Add(new PhysicalPackageFile { SourcePath = httpContext.Server.MapPath(cleanFile), TargetPath = "lib/" + Regex.Replace(cleanFile, "^~/Bin/", "", RegexOptions.IgnoreCase) }); } } else { if (isDirectory) { CopyFolderToPackage(builder, appContext, httpContext.Server.MapPath("~/"), cleanFilePath, (rootPath, path) => "Content/" + path.TrimStart(rootPath).Replace('\\', '/')); } else { builder.Files.Add(new PhysicalPackageFile { SourcePath = httpContext.Server.MapPath(cleanFile), TargetPath = "Content/" + Regex.Replace(cleanFile, "^~/", "", RegexOptions.IgnoreCase) }); } } } // Web.config if (!string.IsNullOrWhiteSpace(packageDef.Config)) { builder.Files.Add(new ByteArrayPackageFile { Contents = Encoding.UTF8.GetBytes(packageDef.Config), TargetPath = "Web.config" }); } // Write package to disc using (Stream stream = File.Create(packageFile.FullName)) { builder.Save(stream); } // If we've gotten this far, everything must have gone ok result.Success = true; } catch (global::System.Exception ex) { result.Success = false; result.ErrorMessage = ex.Message; } return(result); }