|
| 1 | +;;***************************************************************************** |
| 2 | +;; |
| 3 | +;; Title: RepositoryOverride.dbl |
| 4 | +;; |
| 5 | +;; Description: Classes and tools to apply repository overrides based on a |
| 6 | +;; metadata in a JSON file. |
| 7 | +;; |
| 8 | +;; Date: 15th January 2020 |
| 9 | +;; |
| 10 | +;; Author: Steve Ives, Synergex Professional Services Group |
| 11 | +;; http://www.synergex.com |
| 12 | +;; |
| 13 | +;;***************************************************************************** |
| 14 | +;; |
| 15 | +;; Copyright (c) 2020, Synergex International, Inc. |
| 16 | +;; All rights reserved. |
| 17 | +;; |
| 18 | +;; Redistribution and use in source and binary forms, with or without |
| 19 | +;; modification, are permitted provided that the following conditions are met: |
| 20 | +;; |
| 21 | +;; * Redistributions of source code must retain the above copyright notice, |
| 22 | +;; this list of conditions and the following disclaimer. |
| 23 | +;; |
| 24 | +;; * Redistributions in binary form must reproduce the above copyright notice, |
| 25 | +;; this list of conditions and the following disclaimer in the documentation |
| 26 | +;; and/or other materials provided with the distribution. |
| 27 | +;; |
| 28 | +;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 29 | +;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 | +;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 | +;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 32 | +;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 33 | +;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 34 | +;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 35 | +;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 36 | +;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 37 | +;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 38 | +;; POSSIBILITY OF SUCH DAMAGE. |
| 39 | +;; |
| 40 | +;;***************************************************************************** |
| 41 | + |
| 42 | +import System |
| 43 | +import System.Collections |
| 44 | +import System.Collections.Generic |
| 45 | +import System.IO |
| 46 | +import System.Linq |
| 47 | +import System.Text |
| 48 | +import CodeGen.RepositoryAPI |
| 49 | +import Newtonsoft.Json |
| 50 | + |
| 51 | +namespace CodeGen.Engine |
| 52 | + |
| 53 | + public static class RepositoryOverride |
| 54 | + |
| 55 | + public static method ApplyOverrides, boolean |
| 56 | + required inout context, @CodeGenContext |
| 57 | + proc |
| 58 | + data errStatus = false |
| 59 | + |
| 60 | + data overrides, @OverrideRepository |
| 61 | + |
| 62 | + ;;Override files must be in the templates folder |
| 63 | + data overrideFile = Path.Combine(context.TemplateFolder,context.CurrentTask.RepositoryOverrideFile) |
| 64 | + |
| 65 | + ;;Does the override file exist? |
| 66 | + if (File.Exists(overrideFile)) then |
| 67 | + begin |
| 68 | + ;;Read and de-serialize the override file |
| 69 | + |
| 70 | + data jsonData = File.ReadAllText(overrideFile) |
| 71 | + |
| 72 | + try |
| 73 | + begin |
| 74 | + overrides = JsonConvert.DeserializeObject<OverrideRepository>(jsonData) |
| 75 | + end |
| 76 | + catch (e, @Exception) |
| 77 | + begin |
| 78 | + errStatus = context.CurrentTask.ErrorLog("Failed to read or deserialize repository override file " + overrideFile + "!") |
| 79 | + end |
| 80 | + endtry |
| 81 | + end |
| 82 | + else |
| 83 | + begin |
| 84 | + errStatus = context.CurrentTask.ErrorLog("Repository override file " + overrideFile + " not found!") |
| 85 | + end |
| 86 | + |
| 87 | + if (!errStatus) |
| 88 | + begin |
| 89 | + ;;Do we have override structures? |
| 90 | + data ostr, @OverrideStructure |
| 91 | + if (overrides.Structures == ^null) then |
| 92 | + begin |
| 93 | + errStatus = context.CurrentTask.ErrorLog("Repository override file contains no structures!") |
| 94 | + end |
| 95 | + else |
| 96 | + begin |
| 97 | + ;;Iterate through the override structures |
| 98 | + foreach ostr in overrides.Structures |
| 99 | + begin |
| 100 | + ;;Find the matching repository structure in the repository |
| 101 | + data str, @RpsStructure, context.Repository.Structures.FirstOrDefault(lambda(s) { s.Name == ostr.Name }) |
| 102 | + if (str == ^null) then |
| 103 | + begin |
| 104 | + errStatus = context.CurrentTask.ErrorLog("Repository override file structure " + ostr.Name + " not found in repository!") |
| 105 | + exitloop |
| 106 | + end |
| 107 | + else |
| 108 | + begin |
| 109 | + ;;Do we have excluded keys? |
| 110 | + if (ostr.ExcludeKeys != ^null) |
| 111 | + begin |
| 112 | + ;;Iterate through the excluded keys |
| 113 | + data excludedKeyName, string |
| 114 | + foreach excludedKeyName in ostr.ExcludeKeys |
| 115 | + begin |
| 116 | + ;;Find the matching repository key in the repository structure |
| 117 | + data key, @RpsKey, str.Keys.FirstOrDefault(lambda(k) { k.Name == excludedKeyName }) |
| 118 | + if (key == ^null) then |
| 119 | + begin |
| 120 | + errStatus = context.CurrentTask.ErrorLog("Repository override file key " + ostr.Name + "." + excludedKeyName + " not found in repository structure " + ostr.Name + "!") |
| 121 | + exitloop |
| 122 | + end |
| 123 | + else |
| 124 | + begin |
| 125 | + ;;And remove it |
| 126 | + str.Keys.Remove(key) |
| 127 | + |
| 128 | + ;;Is the structure selected for processing in this task? |
| 129 | + data str2, @RpsStructure, context.Structures.FirstOrDefault(lambda(s) { s.Name == ostr.Name }) |
| 130 | + if (str2 != ^null) |
| 131 | + begin |
| 132 | + ;;Find the matching repository key in the repository structure |
| 133 | + key = str2.Keys.FirstOrDefault(lambda(k) { k.Name == excludedKeyName }) |
| 134 | + str2.Keys.Remove(key) |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + mreturn errStatus |
| 145 | + |
| 146 | + endmethod |
| 147 | + |
| 148 | + ; |
| 149 | + ; { |
| 150 | + ; "Structures": [ |
| 151 | + ; { |
| 152 | + ; "Name": "STR1", |
| 153 | + ; "ExcludeKeys": [ "KEY1", "KEY2" ] |
| 154 | + ; }, |
| 155 | + ; { |
| 156 | + ; "Name": "STR2", |
| 157 | + ; "ExcludeKeys": [ "KEY1", "KEY2" ] |
| 158 | + ; } |
| 159 | + ; ] |
| 160 | + ; } |
| 161 | + ; |
| 162 | + |
| 163 | + private class OverrideRepository |
| 164 | + public readwrite property Structures, @IEnumerable<OverrideStructure> |
| 165 | + endclass |
| 166 | + |
| 167 | + private class OverrideStructure |
| 168 | + public readwrite property Name, string |
| 169 | + public readwrite property ExcludeKeys, [#]string |
| 170 | + endclass |
| 171 | + |
| 172 | + endclass |
| 173 | + |
| 174 | + |
| 175 | +endnamespace |
0 commit comments