private static void ExtractCanvasApp(string appDirectory, Args.Args options) { var codeDirectory = Path.Combine(appDirectory, Paths.Code); var controlsDir = Path.Combine(appDirectory, Paths.Controls); var autoValueExtractor = new AutoValueExtractor(); var header = File.ReadAllText(Path.Combine(appDirectory, Paths.Header)); var indexOfDocVersion = header.IndexOf(DocVersionStartText, StringComparison.InvariantCultureIgnoreCase) + DocVersionStartText.Length; var version = new Version(header[indexOfDocVersion
private static CanvasAppScreen PackScreen(string codeDirectory, AutoValueExtractor extractor) { var jsonFile = Path.Combine(codeDirectory, Path.GetFileName(codeDirectory)) + ".json"; Logger.Log("Parsing file " + Path.GetFileNameWithoutExtension(jsonFile)); var json = File.ReadAllText(jsonFile); var screen = JsonConvert.DeserializeObject <CanvasAppScreen>(json); Logger.Log("Packaging file " + screen.TopParent.Name + " from " + Path.GetFileNameWithoutExtension(jsonFile)); PackChildControl(screen.TopParent, jsonFile, extractor); return(screen); }
private static void PackScreens(string appPath, string zipPath, AutoValueExtractor extractor) { foreach (var codeDirectory in Directory.GetDirectories(Path.Combine(appPath, UnpackLogic.Paths.Code))) { var screen = PackScreen(codeDirectory, extractor); var dir = Path.Combine(zipPath, UnpackLogic.Paths.Controls); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.WriteAllText(Path.Combine(dir, screen.TopParent.ControlUniqueId) + ".json", screen.Serialize(Formatting.Indented)); } }
private static void PackChildren(IControl control, string jsonFile, AutoValueExtractor extractor) { var childrenByName = new Dictionary <string, Child>(); foreach (var childDirectory in Directory.GetDirectories(Path.GetDirectoryName(jsonFile))) { var childJsonFile = Path.Combine(childDirectory, Path.GetFileName(childDirectory)) + ".json"; var child = JsonConvert.DeserializeObject <Child>(File.ReadAllText(childJsonFile)); PackChildControl(child, childJsonFile, extractor); childrenByName.Add(child.Name, child); } if (childrenByName.Count == 0) { if (control.ChildrenOrder?.Count > 0) { throw new Exception($"Unable to find child control \"{control.ChildrenOrder.First().Name}\" specified in children order for \"{control.Name}\"."); } return; } var newChildList = new List <Child>(); foreach (var child in control.ChildrenOrder) { if (childrenByName.TryGetValue(child.Name, out var fullChild)) { newChildList.Add(fullChild); childrenByName.Remove(child.Name); } else { throw new Exception($"Unable to find child control \"{child.Name}\" specified in children order for \"{control.Name}\"."); } } if (childrenByName.Count > 0) { throw new Exception($"No ChildrenOrder specified for \"{childrenByName.First().Key}\" in \"{control.Name}\"."); } control.Children = newChildList; control.ChildrenOrder = null; }
private static void PackApp(string appPath, string mainAppPath) { Logger.Log("Processing App at: " + appPath); var zipPath = GetTemporaryDirectory(); try { Logger.Log("Parsing Auto Values"); var extractor = AutoValueExtractor.Parse(Path.Combine(appPath, UnpackLogic.Paths.Code, UnpackLogic.Paths.AutoValues) + UnpackLogic.DataFileExt); Logger.Log("Copying app files for zip creation."); CopyFilesToZipFolder(appPath, zipPath, "MsApp", UnpackLogic.Paths.Code, UnpackLogic.Paths.Metadata); Logger.Log("Packaging Code files"); PackScreens(appPath, zipPath, extractor); Logger.Log("Parsing AppInfo"); var sourcePath = Path.Combine(appPath, UnpackLogic.Paths.Metadata); string msAppZipPath; if (Directory.Exists(sourcePath)) { var metadataFiles = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories); var appInfo = AppInfo.Parse(File.ReadAllText(metadataFiles.Single(f => Path.GetExtension(f) == ".json"))); var destinationPath = Path.Combine(mainAppPath, UnpackLogic.Paths.MsPowerApps, "apps", appInfo.AppId); MoveMetadataFilesFromExtract(appInfo, sourcePath, destinationPath, metadataFiles); msAppZipPath = Path.Combine(destinationPath, Path.GetFileName(appInfo.MsAppPath)); } else { msAppZipPath = mainAppPath; } Logger.Log("Parsing Resource\\PublisherInfo"); RestoreAutoNamedFiles(zipPath); Logger.Log($"Packing file {msAppZipPath}"); MsAppHelper.CreateFromDirectory(zipPath, msAppZipPath); } finally { Directory.Delete(zipPath, true); } }
private static void PackScreens(string appPath, string zipPath, string pathCode, string pathControls) { if (!Directory.Exists(Path.Combine(appPath, pathCode))) { return; } Logger.Log("Parsing Auto Values"); var extractor = AutoValueExtractor.Parse(Path.Combine(appPath, pathCode, UnpackLogic.Paths.AutoValues) + UnpackLogic.DataFileExt); foreach (var codeDirectory in Directory.GetDirectories(Path.Combine(appPath, pathCode))) { var screen = PackScreen(codeDirectory, extractor); var dir = Path.Combine(zipPath, pathControls); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.WriteAllText(Path.Combine(dir, screen.TopParent.ControlUniqueId) + ".json", screen.Serialize(Formatting.Indented)); } }
private static void PackChildControl(IControl control, string jsonFile, AutoValueExtractor extractor) { extractor.Inject(control, jsonFile); if (control.Template?.ComponentDefinitionInfo?.Children != null) { extractor.InjectComponentChildren(control.Template.ComponentDefinitionInfo.Children, jsonFile); } Logger.Log("Packing Control " + control.Name); var code = File.ReadAllText(Path.Combine(Path.GetDirectoryName(jsonFile), Path.GetFileNameWithoutExtension(jsonFile)) + UnpackLogic.CodeFileExt); var propertiesByName = new Dictionary <string, string>(); var ruleCodes = code.Replace(Environment.NewLine + "\t", Environment.NewLine) .Split(Environment.NewLine + UnpackLogic.EndOfRuleCode); var endOfNameLine = "(){" + Environment.NewLine; foreach (var ruleCode in ruleCodes) { var index = ruleCode.IndexOf(endOfNameLine, StringComparison.Ordinal); if (index == -1) { break; } var name = ruleCode.Substring(0, index).Split(Environment.NewLine).Last(); propertiesByName.Add(name, ruleCode.Substring(index + endOfNameLine.Length)); } foreach (var property in control.Rules) { if (propertiesByName.TryGetValue(property.Property, out var value)) { property.InvariantScript = value; extractor.Inject(property, jsonFile); } } PackChildren(control, jsonFile, extractor); }
private static void ExtraCodeful(string controlsDir, string codeDirectory, Version version, Args.Args options) { if (!Directory.Exists(controlsDir)) { return; } var autoValueExtractor = new AutoValueExtractor(); foreach (var file in Directory.GetFiles(controlsDir)) { Logger.Log("Extracting file " + file); var json = File.ReadAllText(file); if (!string.IsNullOrWhiteSpace(options.RenameCopiedControlOldPostfix)) { json = RenameControls(json, options); } var screen = JsonConvert.DeserializeObject <CanvasAppScreen>(json); VerifySerialization(screen, json, file, version); var fileDirectory = Path.Combine(codeDirectory, screen.TopParent.Name); ParseControl(screen, screen.TopParent, fileDirectory, autoValueExtractor); } File.WriteAllText(Path.Combine(codeDirectory, Paths.AutoValues) + DataFileExt, autoValueExtractor.Serialize()); Directory.Delete(controlsDir, true); }
private static void ParseControl(CanvasAppScreen screen, IControl control, string directory, AutoValueExtractor autoValueExtractor) { autoValueExtractor.PushControl(control.Name); Directory.CreateDirectory(directory); var sb = new StringBuilder(); // Write out all Rules foreach (var rule in control.Rules) { autoValueExtractor.Extract(rule); sb.AppendLine(rule.Property + "(){"); sb.AppendLine("\t" + rule.InvariantScript.Replace(Environment.NewLine, Environment.NewLine + "\t")); sb.AppendLine(EndOfRuleCode + rule.Property + Environment.NewLine); rule.InvariantScript = null; } File.WriteAllText(Path.Join(directory, control.Name) + CodeFileExt, sb.ToString()); // Create List of Children so the order can be maintained var childrenOrder = new List <ChildOrder>(); // Write out all Children Rules foreach (var child in control.Children) { ParseControl(screen, child, Path.Combine(directory, child.Name), autoValueExtractor); childrenOrder.Add(new ChildOrder { Name = child.Name, ChildrenOrder = child.ChildrenOrder }); } if (control.Template?.ComponentDefinitionInfo?.Children != null) { autoValueExtractor.ExtractComponentChildren(control.Template.ComponentDefinitionInfo.Children); } control.Children = null; control.ChildrenOrder = childrenOrder.Count == 0 ? null : childrenOrder; autoValueExtractor.Extract(control); File.WriteAllText(Path.Combine(directory, Path.GetFileName(directory)) + DataFileExt, screen.TopParent == control ? screen.Serialize(Formatting.Indented) : control.Serialize(Formatting.Indented)); autoValueExtractor.PopControl(); }