Beispiel #1
0
        public void LoadExternalRuleCollections()
        {
            ExternalRuleCollections = new List <BestPracticeCollection>();
            if (_model != null)
            {
                var externalRuleCollectionsJson = _model.GetAnnotation(BPAAnnotationExternalRules);
                if (externalRuleCollectionsJson != null)
                {
                    try
                    {
                        var externalRuleFilePaths = JsonConvert.DeserializeObject <List <string> >(externalRuleCollectionsJson);

                        Environment.CurrentDirectory = FileSystemHelper.DirectoryFromPath(UIController.Current.File_Current) ?? Environment.CurrentDirectory;

                        foreach (var filePath in externalRuleFilePaths)
                        {
                            try
                            {
                                if (filePath.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    ExternalRuleCollections.Add(BestPracticeCollection.GetCollectionFromUrl(filePath));
                                }
                                else
                                {
                                    ExternalRuleCollections.Add(BestPracticeCollection.GetCollectionFromFile(filePath));
                                }
                            }
                            catch { }
                        }
                    }
                    catch { }
                }
            }
        }
        public static bool Show(BestPracticeCollection source, bool plural, Analyzer analyzer, out BestPracticeCollection destinationCollection, out bool clone)
        {
            var form = new MoveToCollectionDialog();

            form.lblHeader.Text  = "Which collection do you want to move the selected rule" + (plural ? "s" : "") + " to?";
            form.chkCopy.Text    = "Clone rule" + (plural ? "s" : "") + " to collection";
            form.chkCopy.Enabled = source.AllowEdit;
            if (!source.AllowEdit)
            {
                form.chkCopy.Checked = true;                    // Only allow copying when sourcing rules from a read-only collection
            }
            form.comboBox1.DisplayMember = "Name";
            form.comboBox1.Items.AddRange(analyzer.Collections.Where(rc => rc.AllowEdit && rc != source).ToArray());
            if (form.comboBox1.Items.Count == 0)
            {
                form.btnOK.Enabled = true;
            }
            else
            {
                form.comboBox1.SelectedIndex = 0;
            }

            destinationCollection = null;
            clone = false;

            if (form.ShowDialog() == DialogResult.OK)
            {
                clone = form.chkCopy.Checked;
                destinationCollection = form.comboBox1.SelectedItem as BestPracticeCollection;
                return(true);
            }
            return(false);
        }
Beispiel #3
0
        public void LoadExternalRuleCollections()
        {
            ExternalRuleCollections = new List <BestPracticeCollection>();
            if (_model != null)
            {
                var externalRuleCollectionsJson = _model.GetAnnotation(BPAAnnotationExternalRules);
                if (externalRuleCollectionsJson != null)
                {
                    try
                    {
                        var externalRuleFilePaths = JsonConvert.DeserializeObject <List <string> >(externalRuleCollectionsJson);

                        foreach (var filePath in externalRuleFilePaths)
                        {
                            try
                            {
                                if (filePath.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    ExternalRuleCollections.Add(BestPracticeCollection.GetCollectionFromUrl(filePath));
                                }
                                else
                                {
                                    ExternalRuleCollections.Add(BestPracticeCollection.GetCollectionFromFile(BasePath, filePath));
                                }
                            }
                            catch { }
                        }
                    }
                    catch { }
                }
            }
        }
Beispiel #4
0
        public bool LocalFile()
        {
            var startDir = FileSystemHelper.DirectoryFromPath(UIController.Current.File_Current) ?? Environment.CurrentDirectory;

            var sfd = new CommonOpenFileDialog("Open Rule File");

            sfd.EnsureFileExists = true;
            sfd.InitialDirectory = startDir;
            sfd.DefaultExtension = "json";
            sfd.Filters.Add(new CommonFileDialogFilter("JSON file", "*.json"));
            sfd.Filters.Add(new CommonFileDialogFilter("All files", "*.*"));

            var localPathCheckBox = new CommonFileDialogCheckBox("Use relative path", true)
            {
                Visible = false
            };

            if (UIController.Current.File_Current != null)
            {
                sfd.Controls.Add(localPathCheckBox);
                localPathCheckBox.Visible = true;
                sfd.FolderChanging       += (s, e) =>
                {
                    var relativePath = FileSystemHelper.GetRelativePath(startDir, e.Folder);
                    if (relativePath.Length >= 2 && relativePath[1] == ':')
                    {
                        localPathCheckBox.Visible = false;
                    }
                    else
                    {
                        localPathCheckBox.Visible = true;
                    }
                };
            }

            parent.Enabled = false;
            if (sfd.ShowDialog() == CommonFileDialogResult.Ok)
            {
                parent.Enabled = true;
                var fileName = localPathCheckBox.Visible && localPathCheckBox.IsChecked
                    ? FileSystemHelper.GetRelativePath(startDir, sfd.FileName) : sfd.FileName;

                if (!analyzer.ExternalRuleCollections.Any(
                        rc => rc.FilePath != null && FileSystemHelper.GetAbsolutePath(analyzer.BasePath, rc.FilePath).EqualsI(sfd.FileName)
                        ))
                {
                    analyzer.ExternalRuleCollections.Add(BestPracticeCollection.GetCollectionFromFile(analyzer.BasePath, fileName));
                }
                return(true);
            }
            parent.Enabled = true;
            return(false);
        }
Beispiel #5
0
        public static BestPracticeCollection GetCollectionFromFile(string filePath)
        {
            var result = new BestPracticeCollection();

            result.Name     = filePath;
            result.FilePath = filePath;

            var fi = new FileInfo(filePath);

            result.AllowEdit = FileSystemHelper.IsDirectoryWritable(fi.DirectoryName);
            result.AddFromJsonFile(filePath);

            return(result);
        }
Beispiel #6
0
        static public BestPracticeCollection GetStandardBestPractices()
        {
            var bpc = new BestPracticeCollection();

            // These should be loaded from GitHub instead

            /*
             * bpc.Add(new BestPracticeRule
             * {
             *  Name = "Do not summarize key columns",
             *  ID = "KEYCOLUMNS_SUMMARIZEBY_NONE",
             *  Scope = RuleScope.CalculatedColumn | RuleScope.CalculatedTableColumn | RuleScope.DataColumn,
             *  Description = "Visible numeric columns whose name end with Key or ID should have their 'Summarize By' property set to 'Do Not Summarize'.",
             *  Severity = 1,
             *  Expression = "(Name.EndsWith(\"Key\", true, null) or Name.EndsWith(\"ID\", true, null)) and SummarizeBy <> \"None\" and not IsHidden and not Table.IsHidden",
             *  FixExpression = "SummarizeBy = TOM.AggregateFunction.None",
             *  Compatibility = new HashSet<int> { 1200, 1400 }
             * });
             *
             * bpc.Add(new BestPracticeRule
             * {
             *  Name = "Hide foreign key columns",
             *  ID = "FKCOLUMNS_HIDDEN",
             *  Scope = RuleScope.CalculatedColumn | RuleScope.CalculatedTableColumn | RuleScope.DataColumn,
             *  Description = "Columns used on the Many side of a relationship should be hidden.",
             *  Severity = 1,
             *  Expression = "Model.Relationships.Any(FromColumn = outerIt) and not IsHidden and not Table.IsHidden",
             *  FixExpression = "IsHidden = true",
             *  Compatibility = new HashSet<int> { 1200, 1400 }
             * });
             *
             * bpc.Add(new BestPracticeRule {
             *  Name = "Test rule",
             *  ID = "TEST_RULE",
             *  Scope = RuleScope.Measure,
             *  Expression = "not RegEx.IsMatch(Table.Name,\"KPIs\") and (not Expression.Contains(\"Test\") or (Expression = \"Hej\" and Expression = \"Dav\") or not Name.StartsWith(\"test\",true,null) or Name <> \"Hej\" or Name <> \"1\" or not (Name <> \"2\") or Name <> \"3\") and Description = \"\"",
             *  Compatibility = new HashSet<int> { 1200, 1400 }
             * });
             *
             * bpc.Add(new BestPracticeRule {
             *  Name = "Test rule 2",
             *  ID = "TEST_RULE_2",
             *  Scope = RuleScope.Measure,
             *  Expression = "!string.IsNullOrEmpty(Table.Name)",
             *  Compatibility = new HashSet<int> { 1200, 1400 }
             * });*/

            return(bpc);
        }
 public bool Url()
 {
     if (UrlInputDialog.Show(out string url))
     {
         if (!analyzer.ExternalRuleCollections.Any(rc => rc.Url != null && rc.Url.EqualsI(url)))
         {
             analyzer.ExternalRuleCollections.Add(BestPracticeCollection.GetCollectionFromUrl(url));
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #8
0
        public static BestPracticeCollection GetCollectionFromUrl(string url)
        {
            var result = new BestPracticeCollection();

            result.AllowEdit = false;
            result.Internal  = false;
            result.Url       = url;
            result.Name      = url;

            try
            {
                result.Rules = LoadFromJson(GetUrl(url));
            }
            catch (Exception ex) { }

            return(result);
        }
Beispiel #9
0
        public static BestPracticeCollection GetCurrentModelCollection(Model model)
        {
            if (model == null)
            {
                return(null);
            }

            var result = new BestPracticeCollection();

            result.model = model;
            var localRulesJson = model.GetAnnotation(BPAAnnotation) ?? model.GetAnnotation("BestPractizeAnalyzer"); // Stupid typo in earlier version

            if (!string.IsNullOrEmpty(localRulesJson))
            {
                result.Rules = LoadFromJson(localRulesJson);
            }
            result.AllowEdit = true;
            result.Name      = "Rules within the current model";
            result.Internal  = true;

            return(result);
        }
Beispiel #10
0
        public Analyzer()
        {
            Model = null;

            GlobalRules = new BestPracticeCollection();

            try
            {
                var p1 = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\TabularEditor\BPARules.json";
                var p2 = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\TabularEditor\BPARules.json";
                if (File.Exists(p1))
                {
                    GlobalRules.AddFromJsonFile(p1);
                }
                if (File.Exists(p2))
                {
                    GlobalRules.AddFromJsonFile(p2);
                }
            }
            catch { }

            //StandardBestPractices.GetStandardBestPractices().SaveToFile(@"c:\Projects\test.json");
        }
Beispiel #11
0
        internal bool HandleCommandLine(string[] args)
        {
            var upperArgList = args.Select(arg => arg.ToUpper()).ToList();
            var argList      = args.Select(arg => arg).ToList();

            if (upperArgList.Contains("-?") || upperArgList.Contains("/?") || upperArgList.Contains("-H") || upperArgList.Contains("/H") || upperArgList.Contains("HELP"))
            {
                OutputUsage();
                return(true);
            }

            EnableVSTS = upperArgList.IndexOf("-VSTS") > -1 || upperArgList.IndexOf("-V") > -1;
            var warnOnUnprocessed = upperArgList.IndexOf("-WARN") > -1 || upperArgList.IndexOf("-W") > -1;
            var errorOnDaxErr     = upperArgList.IndexOf("-ERR") > -1 || upperArgList.IndexOf("-E") > -1;

            TabularModelHandler h;

            if (args.Length == 2 || args[2].StartsWith("-"))
            {
                // File argument provided (either alone or with switches), i.e.:
                //      TabularEditor.exe myfile.bim
                //      TabularEditor.exe myfile.bim -...

                if (!File.Exists(args[1]) && !File.Exists(args[1] + "\\database.json"))
                {
                    Error("File not found: {0}", args[1]);
                    return(true);
                }
                else
                {
                    // If nothing else was specified on the command-line, open the UI:
                    if (args.Length == 2)
                    {
                        return(false);
                    }
                }

                try
                {
                    h = new TOMWrapper.TabularModelHandler(args[1]);
                }
                catch (Exception e)
                {
                    Error("Error loading file: " + e.Message);
                    return(true);
                }
            }
            else if (args.Length == 3 || args[3].StartsWith("-"))
            {
                // Server + Database argument provided (either alone or with switches), i.e.:
                //      TabularEditor.exe localhost AdventureWorks
                //      TabularEditor.exe localhost AdventureWorks -...
                // If nothing else was specified on the command-line, open the UI:
                if (args.Length == 3)
                {
                    return(false);
                }

                try
                {
                    h = new TOMWrapper.TabularModelHandler(args[1], args[2]);
                }
                catch (Exception e)
                {
                    Error("Error loading model: " + e.Message);
                    return(true);
                }
            }
            else
            {
                // Otherwise, it's nonsensical
                return(false);
            }

            string script     = null;
            string scriptFile = null;

            var    doTestRun   = upperArgList.IndexOf("-T");
            string testRunFile = null;

            if (doTestRun == -1)
            {
                doTestRun = upperArgList.IndexOf("-TRX");
            }
            if (doTestRun > -1)
            {
                if (upperArgList.Count <= doTestRun || upperArgList[doTestRun + 1].StartsWith("-"))
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                Program.testRun = new TestRun(h.Database?.Name ?? h.Source);
                testRunFile     = argList[doTestRun + 1];
            }

            var doScript = upperArgList.IndexOf("-SCRIPT");

            if (doScript == -1)
            {
                doScript = upperArgList.IndexOf("-S");
            }
            if (doScript > -1)
            {
                if (upperArgList.Count <= doScript)
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                scriptFile = argList[doScript + 1];
                script     = File.Exists(scriptFile) ? File.ReadAllText(scriptFile) : scriptFile;
            }

            var doCheckDs = upperArgList.IndexOf("-SCHEMACHECK");

            if (doCheckDs == -1)
            {
                doCheckDs = upperArgList.IndexOf("-SC");
            }

            string saveToFolderOutputPath = null;
            string saveToFolderReplaceId  = null;

            var doSaveToFolder = upperArgList.IndexOf("-FOLDER");

            if (doSaveToFolder == -1)
            {
                doSaveToFolder = upperArgList.IndexOf("-F");
            }
            if (doSaveToFolder > -1)
            {
                if (upperArgList.Count <= doSaveToFolder)
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                saveToFolderOutputPath = argList[doSaveToFolder + 1];
                if (doSaveToFolder + 2 < argList.Count && !argList[doSaveToFolder + 2].StartsWith("-"))
                {
                    saveToFolderReplaceId = argList[doSaveToFolder + 2];
                }
                var directoryName = new FileInfo(saveToFolderOutputPath).Directory.FullName;
                Directory.CreateDirectory(saveToFolderOutputPath);
            }

            string buildOutputPath = null;
            string buildReplaceId  = null;

            var doSave = upperArgList.IndexOf("-BUILD");

            if (doSave == -1)
            {
                doSave = upperArgList.IndexOf("-B");
            }
            if (doSave == -1)
            {
                doSave = upperArgList.IndexOf("-BIM");
            }
            if (doSave > -1)
            {
                if (upperArgList.Count <= doSave)
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                buildOutputPath = argList[doSave + 1];
                if (doSave + 2 < argList.Count && !argList[doSave + 2].StartsWith("-"))
                {
                    buildReplaceId = argList[doSave + 2];
                }
                var directoryName = new FileInfo(buildOutputPath).Directory.FullName;
                Directory.CreateDirectory(directoryName);
            }

            if (doSaveToFolder > -1 && doSave > -1)
            {
                Error("-FOLDER and -BUILD arguments are mutually exclusive.\n");
                OutputUsage();
                return(true);
            }

            // Load model:
            Console.WriteLine("Loading model...");

            if (!string.IsNullOrEmpty(script))
            {
                Console.WriteLine("Executing script...");

                System.CodeDom.Compiler.CompilerResults result;
                Scripting.ScriptOutputForm.Reset(false);
                var dyn = ScriptEngine.CompileScript(script, out result);
                //nUnit.StartSuite("Script Compilation");
                if (result.Errors.Count > 0)
                {
                    Error("Script compilation errors:");
                    var errIndex = 0;
                    foreach (System.CodeDom.Compiler.CompilerError err in result.Errors)
                    {
                        errIndex++;
                        ErrorX(err.ErrorText, scriptFile, err.Line, err.Column, err.ErrorNumber);
                        //nUnit.Failure("Script Compilation", $"Compilation Error #{errIndex}", err.ErrorText, $"{scriptFile} line {err.Line}, column {err.Column}");
                    }
                    return(true);
                }
                try
                {
                    h.BeginUpdate("script");
                    dyn.Invoke(h.Model, null);
                    h.EndUpdateAll();
                }
                catch (Exception ex)
                {
                    Error("Script execution error: " + ex.Message);
                    return(true);
                }
                finally
                {
                    h.Model.Database.CloseReader();
                }
            }

            if (doCheckDs > -1)
            {
                Console.WriteLine("Checking source schema...");
                ScriptHelper.SchemaCheck(h.Model);
            }

            if (!string.IsNullOrEmpty(buildOutputPath))
            {
                Console.WriteLine("Building Model.bim file...");
                if (buildReplaceId != null)
                {
                    h.Database.Name = buildReplaceId; h.Database.ID = buildReplaceId;
                }
                h.Save(buildOutputPath, SaveFormat.ModelSchemaOnly, SerializeOptions.Default);
            }
            else if (!string.IsNullOrEmpty(saveToFolderOutputPath))
            {
                Console.WriteLine("Saving Model.bim file to Folder Output Path ...");
                if (buildReplaceId != null)
                {
                    h.Database.Name = buildReplaceId; h.Database.ID = buildReplaceId;
                }

                //Note the last parameter, we use whatever SerializeOptions are already in the file
                h.Save(saveToFolderOutputPath, SaveFormat.TabularEditorFolder, null, true);
            }

            var replaceMap = new Dictionary <string, string>();

            var analyze = upperArgList.IndexOf("-ANALYZE");

            if (analyze == -1)
            {
                analyze = upperArgList.IndexOf("-A");
            }
            if (analyze > -1)
            {
                var rulefile = analyze + 1 < argList.Count ? argList[analyze + 1] : "";
                if (rulefile.StartsWith("-") || string.IsNullOrEmpty(rulefile))
                {
                    rulefile = null;
                }

                Console.WriteLine("Running Best Practice Analyzer...");
                Console.WriteLine("=================================");

                var analyzer = new BPA.Analyzer();
                analyzer.SetModel(h.Model, h.SourceType == ModelSourceType.Database ? null : FileSystemHelper.DirectoryFromPath(h.Source));

                BPA.BestPracticeCollection suppliedRules = null;
                if (!string.IsNullOrEmpty(rulefile))
                {
                    if (File.Exists(rulefile))
                    {
                        try
                        {
                            suppliedRules = BPA.BestPracticeCollection.GetCollectionFromFile(Environment.CurrentDirectory, rulefile);
                        }
                        catch
                        {
                            Error("Invalid rulefile: {0}", rulefile);
                            return(true);
                        }
                    }
                    else
                    {
                        suppliedRules = BPA.BestPracticeCollection.GetCollectionFromUrl(rulefile);
                        if (suppliedRules.Count == 0)
                        {
                            Error("No rules defined in specified URL: {0}", rulefile);
                            return(true);
                        }
                    }
                }

                IEnumerable <BPA.AnalyzerResult> bpaResults;
                if (suppliedRules == null)
                {
                    bpaResults = analyzer.AnalyzeAll();
                }
                else
                {
                    var effectiveRules = analyzer.GetEffectiveRules(false, false, true, true, suppliedRules);
                    bpaResults = analyzer.Analyze(effectiveRules);
                }

                bool none = true;
                foreach (var res in bpaResults.Where(r => !r.Ignored))
                {
                    if (res.InvalidCompatibilityLevel)
                    {
                        Console.WriteLine("Skipping rule '{0}' as it does not apply to Compatibility Level {1}.", res.RuleName, h.CompatibilityLevel);
                    }
                    else
                    if (res.RuleHasError)
                    {
                        none = false;
                        Error("Error on rule '{0}': {1}", res.RuleName, res.RuleError);
                    }
                    else
                    {
                        none = false;
                        if (res.Object != null)
                        {
                            var text = string.Format("{0} {1} violates rule \"{2}\"",
                                                     res.Object.GetTypeName(),
                                                     (res.Object as IDaxObject)?.DaxObjectFullName ?? res.ObjectName,
                                                     res.RuleName
                                                     );
                            if (res.Rule.Severity <= 1)
                            {
                                Console.WriteLine(text);
                            }
                            else if (res.Rule.Severity == 2)
                            {
                                Warning(text);
                            }
                            else if (res.Rule.Severity >= 3)
                            {
                                Error(text);
                            }
                        }
                    }
                }
                if (none)
                {
                    Console.WriteLine("No objects in violation of Best Practices.");
                }
                Console.WriteLine("=================================");
            }

            var deploy = upperArgList.IndexOf("-DEPLOY");

            if (deploy == -1)
            {
                deploy = upperArgList.IndexOf("-D");
            }
            if (deploy > -1)
            {
                var serverName = argList.Skip(deploy + 1).FirstOrDefault(); if (serverName == null || serverName.StartsWith("-"))
                {
                    serverName = null;
                }
                var databaseID = argList.Skip(deploy + 2).FirstOrDefault(); if (databaseID != null && databaseID.StartsWith("-"))
                {
                    databaseID = null;
                }

                // Perform direct save:
                if (serverName == null)
                {
                    var nextSwitch     = upperArgList.Skip(deploy + 1).FirstOrDefault();
                    var deploySwitches = new[] { "-L", "-LOGIN", "-O", "-OVERWRITE", "-C", "-CONNECTIONS", "-P", "-PARTITIONS", "-R", "-ROLES", "-M", "-MEMBERS", "-X", "-XMLA" };
                    if (deploySwitches.Contains(nextSwitch))
                    {
                        Error("Invalid argument syntax.\n");
                        OutputUsage();
                        return(true);
                    }

                    Console.WriteLine("Saving model metadata back to source...");
                    if (h.SourceType == ModelSourceType.Database)
                    {
                        try
                        {
                            h.SaveDB();
                            Console.WriteLine("Model metadata saved.");

                            var deploymentResult = h.GetLastDeploymentResults();
                            foreach (var err in deploymentResult.Issues)
                            {
                                if (errorOnDaxErr)
                                {
                                    Error(err);
                                }
                                else
                                {
                                    Warning(err);
                                }
                            }
                            foreach (var err in deploymentResult.Warnings)
                            {
                                Warning(err);
                            }
                            foreach (var err in deploymentResult.Unprocessed)
                            {
                                if (warnOnUnprocessed)
                                {
                                    Warning(err);
                                }
                                else
                                {
                                    Console.WriteLine(err);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Error("Save failed: " + ex.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            h.Save(h.Source, h.SourceType == ModelSourceType.Folder ? SaveFormat.TabularEditorFolder : h.SourceType == ModelSourceType.Pbit ? SaveFormat.PowerBiTemplate : SaveFormat.ModelSchemaOnly, h.SerializeOptions, true);
                            Console.WriteLine("Model metadata saved.");
                        }
                        catch (Exception ex)
                        {
                            Error("Save failed: " + ex.Message);
                        }
                    }
                    return(true);
                }

                var conn = upperArgList.IndexOf("-CONNECTIONS");
                if (conn == -1)
                {
                    conn = upperArgList.IndexOf("-C");
                }
                if (conn > -1)
                {
                    var replaces = argList.Skip(conn + 1).TakeWhile(s => s[0] != '-').ToList();

                    if (replaces.Count > 0 && replaces.Count % 2 == 0)
                    {
                        // Placeholder replacing:
                        for (var index = 0; index < replaces.Count; index = index + 2)
                        {
                            replaceMap.Add(replaces[index], replaces[index + 1]);
                        }
                    }
                }

                string userName = null;
                string password = null;
                var    options  = DeploymentOptions.StructureOnly;

                var switches = args.Skip(deploy + 1).Where(arg => arg.StartsWith("-")).Select(arg => arg.ToUpper()).ToList();

                if (string.IsNullOrEmpty(serverName) || string.IsNullOrEmpty(databaseID))
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                if (switches.Contains("-L") || switches.Contains("-LOGIN"))
                {
                    var switchPos = upperArgList.IndexOf("-LOGIN"); if (switchPos == -1)
                    {
                        switchPos = upperArgList.IndexOf("-L");
                    }
                    userName = argList.Skip(switchPos + 1).FirstOrDefault(); if (userName != null && userName.StartsWith("-"))
                    {
                        userName = null;
                    }
                    password = argList.Skip(switchPos + 2).FirstOrDefault(); if (password != null && password.StartsWith("-"))
                    {
                        password = null;
                    }
                    if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                    {
                        Error("Missing username or password.\n");
                        OutputUsage();
                        return(true);
                    }
                    switches.Remove("-L"); switches.Remove("-LOGIN");
                }
                if (switches.Contains("-O") || switches.Contains("-OVERWRITE"))
                {
                    options.DeployMode = DeploymentMode.CreateOrAlter;
                    switches.Remove("-O"); switches.Remove("-OVERWRITE");
                }
                else
                {
                    options.DeployMode = DeploymentMode.CreateDatabase;
                }
                if (switches.Contains("-P") || switches.Contains("-PARTITIONS"))
                {
                    options.DeployPartitions = true;
                    switches.Remove("-P"); switches.Remove("-PARTITIONS");
                }
                if (switches.Contains("-C") || switches.Contains("-CONNECTIONS"))
                {
                    options.DeployConnections = true;
                    switches.Remove("-C"); switches.Remove("-CONNECTIONS");
                }
                if (switches.Contains("-R") || switches.Contains("-ROLES"))
                {
                    options.DeployRoles = true;
                    switches.Remove("-R"); switches.Remove("-ROLES");

                    if (switches.Contains("-M") || switches.Contains("-MEMBERS"))
                    {
                        options.DeployRoleMembers = true;
                        switches.Remove("-M"); switches.Remove("-MEMBERS");
                    }
                }
                var    xmla_scripting_only = switches.Contains("-X") || switches.Contains("-XMLA");
                string xmla_script_file    = null;
                if (xmla_scripting_only)
                {
                    var switchPos = upperArgList.IndexOf("-XMLA"); if (switchPos == -1)
                    {
                        switchPos = upperArgList.IndexOf("-X");
                    }
                    xmla_script_file = argList.Skip(switchPos + 1).FirstOrDefault(); if (String.IsNullOrWhiteSpace(xmla_script_file) || xmla_script_file.StartsWith("-"))
                    {
                        xmla_script_file = null;
                    }
                    if (string.IsNullOrEmpty(xmla_script_file))
                    {
                        Error("Missing xmla_script_file.\n");
                        OutputUsage();
                        return(true);
                    }
                    switches.Remove("-X");
                    switches.Remove("-XMLA");
                }

                /*if(switches.Count > 0)
                 * {
                 *  Error("Unknown switch {0}\n", switches[0]);
                 *  OutputUsage();
                 *  return true;
                 * }*/

                try
                {
                    if (replaceMap.Count > 0)
                    {
                        Console.WriteLine("Switching connection string placeholders...");
                        foreach (var map in replaceMap)
                        {
                            h.Model.DataSources.SetPlaceholder(map.Key, map.Value);
                        }
                    }

                    var cs = string.IsNullOrEmpty(userName) ? TabularConnection.GetConnectionString(serverName) :
                             TabularConnection.GetConnectionString(serverName, userName, password);
                    if (xmla_scripting_only)
                    {
                        Console.WriteLine("Generating XMLA/TMSL script...");
                        var s = new TOM.Server();
                        s.Connect(cs);
                        var xmla = TabularDeployer.GetTMSL(h.Database, s, databaseID, options);
                        using (var sw = new StreamWriter(xmla_script_file))
                        {
                            sw.Write(xmla);
                        }
                        Console.WriteLine("XMLA/TMSL script generated.");
                    }
                    else
                    {
                        Console.WriteLine("Deploying...");
                        h.Model.UpdateDeploymentMetadata(DeploymentModeMetadata.CLI);
                        var deploymentResult = TabularDeployer.Deploy(h, cs, databaseID, options);
                        Console.WriteLine("Deployment succeeded.");
                        foreach (var err in deploymentResult.Issues)
                        {
                            if (errorOnDaxErr)
                            {
                                Error(err);
                            }
                            else
                            {
                                Warning(err);
                            }
                        }
                        foreach (var err in deploymentResult.Warnings)
                        {
                            Warning(err);
                        }
                        foreach (var err in deploymentResult.Unprocessed)
                        {
                            if (warnOnUnprocessed)
                            {
                                Warning(err);
                            }
                            else
                            {
                                Console.WriteLine(err);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Error($"{(xmla_scripting_only ? "Script generation" : "Deployment")} failed! {ex.Message}");
                }
            }
            if (Program.testRun != null)
            {
                Program.testRun.SerializeAsVSTest(testRunFile);
                Console.WriteLine("VSTest XML file saved: " + testRunFile);
            }

            return(true);
        }
Beispiel #12
0
 public void LoadInternalRules()
 {
     LocalMachineRules = BestPracticeCollection.GetLocalMachineCollection();
     LocalUserRules    = BestPracticeCollection.GetLocalUserCollection();
 }
Beispiel #13
0
 public void LoadModelRules()
 {
     ModelRules = BestPracticeCollection.GetCurrentModelCollection(_model);
 }
        static bool HandleCommandLine(string[] args)
        {
            var upperArgList = args.Select(arg => arg.ToUpper()).ToList();
            var argList      = args.Select(arg => arg).ToList();

            if (upperArgList.Contains("-?") || upperArgList.Contains("/?") || upperArgList.Contains("-H") || upperArgList.Contains("/H") || upperArgList.Contains("HELP"))
            {
                OutputUsage();
                return(true);
            }

            enableVSTS = upperArgList.IndexOf("-VSTS") > -1 || upperArgList.IndexOf("-V") > -1;
            var warnOnUnprocessed = upperArgList.IndexOf("-WARN") > -1 || upperArgList.IndexOf("-W") > -1;

            TabularModelHandler h;

            if (args.Length == 2 || args[2].StartsWith("-"))
            {
                // File argument provided (either alone or with switches), i.e.:
                //      TabularEditor.exe myfile.bim
                //      TabularEditor.exe myfile.bim -...

                if (!File.Exists(args[1]) && !File.Exists(args[1] + "\\database.json"))
                {
                    Error("File not found: {0}", args[1]);
                    return(true);
                }
                else
                {
                    // If nothing else was specified on the command-line, open the UI:
                    if (args.Length == 2)
                    {
                        return(false);
                    }
                }

                try
                {
                    h = new TOMWrapper.TabularModelHandler(args[1]);
                }
                catch (Exception e)
                {
                    Error("Error loading file: " + e.Message);
                    return(true);
                }
            }
            else if (args.Length == 3 || args[3].StartsWith("-"))
            {
                // Server + Database argument provided (either alone or with switches), i.e.:
                //      TabularEditor.exe localhost AdventureWorks
                //      TabularEditor.exe localhost AdventureWorks -...
                // If nothing else was specified on the command-line, open the UI:
                if (args.Length == 3)
                {
                    return(false);
                }

                try
                {
                    h = new TOMWrapper.TabularModelHandler(args[1], args[2]);
                }
                catch (Exception e)
                {
                    Error("Error loading model: " + e.Message);
                    return(true);
                }
            }
            else
            {
                // Otherwise, it's nonsensical
                return(false);
            }

            string script     = null;
            string scriptFile = null;

            var doScript = upperArgList.IndexOf("-SCRIPT");

            if (doScript == -1)
            {
                doScript = upperArgList.IndexOf("-S");
            }
            if (doScript > -1)
            {
                if (upperArgList.Count <= doScript)
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                scriptFile = argList[doScript + 1];
                if (!File.Exists(scriptFile))
                {
                    Error("Specified script file not found.\n");
                    return(true);
                }

                script = File.ReadAllText(scriptFile);
            }

            string buildOutputPath = null;

            var doSave = upperArgList.IndexOf("-BUILD");

            if (doSave == -1)
            {
                doSave = upperArgList.IndexOf("-B");
            }
            if (doSave > -1)
            {
                if (upperArgList.Count <= doSave)
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                buildOutputPath = argList[doSave + 1];
                var directoryName = new FileInfo(buildOutputPath).Directory.FullName;
                Directory.CreateDirectory(directoryName);
            }

            // Load model:
            cw.WriteLine("Loading model...");

            if (!string.IsNullOrEmpty(script))
            {
                cw.WriteLine("Executing script...");

                System.CodeDom.Compiler.CompilerResults result;
                Scripting.ScriptOutputForm.Reset(false);
                var dyn = ScriptEngine.CompileScript(script, out result);
                if (result.Errors.Count > 0)
                {
                    cw.WriteLine("Script compilation errors:");
                    foreach (System.CodeDom.Compiler.CompilerError err in result.Errors)
                    {
                        ErrorX(err.ErrorText, scriptFile, err.Line, err.Column, err.ErrorNumber);
                    }
                    return(true);
                }
                try
                {
                    dyn.Invoke(h.Model, null);
                }
                catch (Exception ex)
                {
                    Error("Script execution error: " + ex.Message);
                    return(true);
                }
            }

            if (!string.IsNullOrEmpty(buildOutputPath))
            {
                cw.WriteLine("Building Model.bim file...");
                h.Save(buildOutputPath, SaveFormat.ModelSchemaOnly, SerializeOptions.Default);
            }

            var replaceMap = new Dictionary <string, string>();

            var analyze = upperArgList.IndexOf("-ANALYZE");

            if (analyze == -1)
            {
                analyze = upperArgList.IndexOf("-A");
            }
            if (analyze > -1)
            {
                var rulefile = argList.Skip(analyze + 1).FirstOrDefault(n => !n.StartsWith("-"));

                var analyzer = new BPA.Analyzer()
                {
                    Model = h.Model
                };

                BPA.BestPracticeCollection suppliedRules = null;
                if (!string.IsNullOrEmpty(rulefile))
                {
                    if (!File.Exists(rulefile))
                    {
                        Error("Rulefile not found: {0}", rulefile);
                        return(true);
                    }
                    try
                    {
                        suppliedRules = BPA.BestPracticeCollection.LoadFromJsonFile(rulefile);
                    }
                    catch
                    {
                        Error("Invalid rulefile: {0}", rulefile);
                        return(true);
                    }
                }

                cw.WriteLine("Running Best Practice Analyzer...");
                cw.WriteLine("=================================");
                IEnumerable <BPA.AnalyzerResult> bpaResults;
                if (suppliedRules == null)
                {
                    bpaResults = analyzer.AnalyzeAll();
                }
                else
                {
                    bpaResults = analyzer.Analyze(suppliedRules.Concat(analyzer.LocalRules));
                }

                if (!bpaResults.Any())
                {
                    cw.WriteLine("No objects in violation of Best Practices.");
                }


                foreach (var res in bpaResults)
                {
                    if (res.RuleHasError)
                    {
                        Warning("Error on rule '{0}': {1}", res.RuleName, res.RuleError);
                    }
                    else
                    {
                        var text = string.Format("{0} {1} violates rule \"{2}\"",
                                                 res.Object.GetTypeName(),
                                                 (res.Object as IDaxObject)?.DaxObjectFullName ?? res.ObjectName,
                                                 res.RuleName
                                                 );
                        if (res.Rule.Severity <= 1)
                        {
                            cw.WriteLine(text);
                        }
                        else if (res.Rule.Severity == 2)
                        {
                            Warning(text);
                        }
                        else if (res.Rule.Severity >= 3)
                        {
                            Error(text);
                        }
                    }
                }
                cw.WriteLine("=================================");
            }

            var deploy = upperArgList.IndexOf("-DEPLOY");

            if (deploy == -1)
            {
                deploy = upperArgList.IndexOf("-D");
            }
            if (deploy > -1)
            {
                var serverName = argList.Skip(deploy + 1).FirstOrDefault(); if (serverName != null && serverName.StartsWith("-"))
                {
                    serverName = null;
                }
                var databaseID = argList.Skip(deploy + 2).FirstOrDefault(); if (databaseID != null && databaseID.StartsWith("-"))
                {
                    databaseID = null;
                }

                var conn = upperArgList.IndexOf("-CONNECTIONS");
                if (conn == -1)
                {
                    conn = upperArgList.IndexOf("-C");
                }
                if (conn > -1)
                {
                    var replaces = argList.Skip(conn + 1).TakeWhile(s => s[0] != '-').ToList();

                    if (replaces.Count > 0 && replaces.Count % 2 == 0)
                    {
                        // Placeholder replacing:
                        for (var index = 0; index < replaces.Count; index = index + 2)
                        {
                            replaceMap.Add(replaces[index], replaces[index + 1]);
                        }
                    }
                }

                string userName = null;
                string password = null;
                var    options  = DeploymentOptions.StructureOnly;

                var switches = args.Skip(deploy + 1).Where(arg => arg.StartsWith("-")).Select(arg => arg.ToUpper()).ToList();

                if (string.IsNullOrEmpty(serverName) || string.IsNullOrEmpty(databaseID))
                {
                    Error("Invalid argument syntax.\n");
                    OutputUsage();
                    return(true);
                }
                if (switches.Contains("-L") || switches.Contains("-LOGIN"))
                {
                    var switchPos = upperArgList.IndexOf("-LOGIN"); if (switchPos == -1)
                    {
                        switchPos = upperArgList.IndexOf("-L");
                    }
                    userName = argList.Skip(switchPos + 1).FirstOrDefault(); if (userName != null && userName.StartsWith("-"))
                    {
                        userName = null;
                    }
                    password = argList.Skip(switchPos + 2).FirstOrDefault(); if (password != null && password.StartsWith("-"))
                    {
                        password = null;
                    }
                    if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                    {
                        Error("Missing username or password.\n");
                        OutputUsage();
                        return(true);
                    }
                    switches.Remove("-L"); switches.Remove("-LOGIN");
                }
                if (switches.Contains("-O") || switches.Contains("-OVERWRITE"))
                {
                    options.DeployMode = DeploymentMode.CreateOrAlter;
                    switches.Remove("-O"); switches.Remove("-OVERWRITE");
                }
                else
                {
                    options.DeployMode = DeploymentMode.CreateDatabase;
                }
                if (switches.Contains("-P") || switches.Contains("-PARTITIONS"))
                {
                    options.DeployPartitions = true;
                    switches.Remove("-P"); switches.Remove("-PARTITIONS");
                }
                if (switches.Contains("-C") || switches.Contains("-CONNECTIONS"))
                {
                    options.DeployConnections = true;
                    switches.Remove("-C"); switches.Remove("-CONNECTIONS");
                }
                if (switches.Contains("-R") || switches.Contains("-ROLES"))
                {
                    options.DeployRoles = true;
                    switches.Remove("-R"); switches.Remove("-ROLES");

                    if (switches.Contains("-M") || switches.Contains("-MEMBERS"))
                    {
                        options.DeployRoleMembers = true;
                        switches.Remove("-M"); switches.Remove("-MEMBERS");
                    }
                }

                /*if(switches.Count > 0)
                 * {
                 *  Error("Unknown switch {0}\n", switches[0]);
                 *  OutputUsage();
                 *  return true;
                 * }*/

                try
                {
                    if (replaceMap.Count > 0)
                    {
                        cw.WriteLine("Switching connection string placeholders...");
                        foreach (var map in replaceMap)
                        {
                            h.Model.DataSources.SetPlaceholder(map.Key, map.Value);
                        }
                    }

                    cw.WriteLine("Deploying...");
                    var cs = string.IsNullOrEmpty(userName) ? TabularConnection.GetConnectionString(serverName) :
                             TabularConnection.GetConnectionString(serverName, userName, password);
                    var deploymentResult = TabularDeployer.Deploy(h, cs, databaseID, options);
                    cw.WriteLine("Deployment succeeded.");
                    foreach (var err in deploymentResult.Issues)
                    {
                        Issue(err);
                    }
                    foreach (var err in deploymentResult.Warnings)
                    {
                        Warning(err);
                    }
                    foreach (var err in deploymentResult.Unprocessed)
                    {
                        if (warnOnUnprocessed)
                        {
                            Warning(err);
                        }
                        else
                        {
                            cw.WriteLine(err);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Error("Deployment failed! " + ex.Message);
                }
                return(true);
            }

            return(true);
        }
Beispiel #15
0
        void AnalyzeBestPracticeRules(string rulefile, bool includeModelRules)
        {
            Console.WriteLine("Running Best Practice Analyzer...");
            Console.WriteLine("=================================");

            var analyzer = new BPA.Analyzer();

            analyzer.SetModel(Handler.Model, Handler.SourceType == ModelSourceType.Database ? null : FileSystemHelper.DirectoryFromPath(Handler.Source));

            BPA.BestPracticeCollection suppliedRules = null;
            if (!string.IsNullOrEmpty(rulefile))
            {
                if (File.Exists(rulefile))
                {
                    try
                    {
                        suppliedRules = BPA.BestPracticeCollection.GetCollectionFromFile(Environment.CurrentDirectory, rulefile);
                    }
                    catch
                    {
                        Error("Invalid rulefile: {0}", rulefile);
                        throw new CommandLineException();
                    }
                }
                else
                {
                    suppliedRules = BPA.BestPracticeCollection.GetCollectionFromUrl(rulefile);
                    if (suppliedRules.Count == 0)
                    {
                        Error("No rules defined in specified URL: {0}", rulefile);
                        throw new CommandLineException();
                    }
                }
            }

            IEnumerable <BPA.AnalyzerResult> bpaResults;

            if (suppliedRules == null)
            {
                // When no rule file is provided, we should also include local user/locale machine rules:
                var effectiveRules = analyzer.GetEffectiveRules(true, true, includeModelRules, includeModelRules);
                bpaResults = analyzer.Analyze(effectiveRules);
            }
            else
            {
                // When a rule file is provided, we ignore local user/locale machine rules:
                var effectiveRules = analyzer.GetEffectiveRules(false, false, includeModelRules, includeModelRules, suppliedRules);
                bpaResults = analyzer.Analyze(effectiveRules);
            }

            bool none = true;

            foreach (var res in bpaResults.Where(r => !r.Ignored))
            {
                if (res.InvalidCompatibilityLevel)
                {
                    Console.WriteLine("Skipping rule '{0}' as it does not apply to Compatibility Level {1}.", res.RuleName, Handler.CompatibilityLevel);
                }
                else
                if (res.RuleHasError)
                {
                    none = false;
                    Error("Error on rule '{0}': {1}", res.RuleName, res.RuleError);
                }
                else
                {
                    none = false;
                    if (res.Object != null)
                    {
                        var text = string.Format("{0} {1} violates rule \"{2}\"",
                                                 res.Object.GetTypeName(),
                                                 (res.Object as IDaxObject)?.DaxObjectFullName ?? res.ObjectName,
                                                 res.RuleName
                                                 );
                        if (res.Rule.Severity <= 1)
                        {
                            Console.WriteLine(text);
                        }
                        else if (res.Rule.Severity == 2)
                        {
                            Warning(text);
                        }
                        else if (res.Rule.Severity >= 3)
                        {
                            Error(text);
                        }
                    }
                }
            }
            if (none)
            {
                Console.WriteLine("No objects in violation of Best Practices.");
            }
            Console.WriteLine("=================================");
        }
Beispiel #16
0
        public bool NewFile()
        {
            var sfd = new CommonSaveFileDialog("New Rule File");

            sfd.EnsurePathExists = true;
            sfd.InitialDirectory = analyzer.BasePath;
            sfd.DefaultFileName  = "BPARules.json";
            sfd.DefaultExtension = "json";
            sfd.Filters.Add(new CommonFileDialogFilter("JSON file", "*.json"));
            sfd.Filters.Add(new CommonFileDialogFilter("All files", "*.*"));

            var localPathCheckBox = new CommonFileDialogCheckBox("Use relative path", true)
            {
                Visible = false
            };

            if (UIController.Current.File_Current != null)
            {
                sfd.Controls.Add(localPathCheckBox);
                localPathCheckBox.Visible = true;
                sfd.FolderChanging       += (s, e) =>
                {
                    var relativePath = FileSystemHelper.GetRelativePath(analyzer.BasePath, e.Folder);
                    if (relativePath.Length >= 2 && relativePath[1] == ':')
                    {
                        localPathCheckBox.Visible = false;
                    }
                    else
                    {
                        localPathCheckBox.Visible = true;
                    }
                };
            }

            parent.Enabled = false;
            if (sfd.ShowDialog() == CommonFileDialogResult.Ok)
            {
                parent.Enabled = true;

                try
                {
                    File.WriteAllText(sfd.FileName, "[]");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Unable to create rule file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(false);
                }

                var fileName = localPathCheckBox.Visible && localPathCheckBox.IsChecked
                    ? FileSystemHelper.GetRelativePath(analyzer.BasePath, sfd.FileName) : sfd.FileName;

                if (!analyzer.ExternalRuleCollections.Any(
                        rc => rc.FilePath != null && FileSystemHelper.GetAbsolutePath(analyzer.BasePath, rc.FilePath).EqualsI(sfd.FileName)
                        ))
                {
                    analyzer.ExternalRuleCollections.Add(BestPracticeCollection.GetCollectionFromFile(analyzer.BasePath, fileName));
                }
                return(true);
            }
            parent.Enabled = true;
            return(false);
        }