forked from MapComplete/MapComplete
		
	Improve validation of tagRenderings
This commit is contained in:
		
							parent
							
								
									ee9646a176
								
							
						
					
					
						commit
						7a68e83afa
					
				
					 1 changed files with 39 additions and 29 deletions
				
			
		|  | @ -4,7 +4,12 @@ import * as licenses from "../assets/generated/license_info.json" | ||||||
| import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson"; | ||||||
| import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson"; | import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson"; | ||||||
| import Constants from "../Models/Constants"; | import Constants from "../Models/Constants"; | ||||||
| import {PrevalidateTheme, ValidateLayer, ValidateThemeAndLayers} from "../Models/ThemeConfig/Conversion/Validation"; | import { | ||||||
|  |     PrevalidateTheme, | ||||||
|  |     ValidateLayer, | ||||||
|  |     ValidateTagRenderings, | ||||||
|  |     ValidateThemeAndLayers | ||||||
|  | } from "../Models/ThemeConfig/Conversion/Validation"; | ||||||
| import {Translation} from "../UI/i18n/Translation"; | import {Translation} from "../UI/i18n/Translation"; | ||||||
| import {TagRenderingConfigJson} from "../Models/ThemeConfig/Json/TagRenderingConfigJson"; | import {TagRenderingConfigJson} from "../Models/ThemeConfig/Json/TagRenderingConfigJson"; | ||||||
| import * as questions from "../assets/tagRenderings/questions.json"; | import * as questions from "../assets/tagRenderings/questions.json"; | ||||||
|  | @ -20,13 +25,13 @@ import {Utils} from "../Utils"; | ||||||
| 
 | 
 | ||||||
| class LayerOverviewUtils { | class LayerOverviewUtils { | ||||||
| 
 | 
 | ||||||
|     writeSmallOverview(themes: { id: string, title: any, shortDescription: any, icon: string, hideFromOverview: boolean, mustHaveLanguage: boolean, layers: (LayerConfigJson | string | {builtin})[] }[]) { |     writeSmallOverview(themes: { id: string, title: any, shortDescription: any, icon: string, hideFromOverview: boolean, mustHaveLanguage: boolean, layers: (LayerConfigJson | string | { builtin })[] }[]) { | ||||||
|         const perId = new Map<string, any>(); |         const perId = new Map<string, any>(); | ||||||
|         for (const theme of themes) { |         for (const theme of themes) { | ||||||
| 
 | 
 | ||||||
|             const keywords : {}[] = [] |             const keywords: {}[] = [] | ||||||
|             for (const layer of (theme.layers ?? [])) { |             for (const layer of (theme.layers ?? [])) { | ||||||
|                 const l = <LayerConfigJson> layer; |                 const l = <LayerConfigJson>layer; | ||||||
|                 keywords.push({"*": l.id}) |                 keywords.push({"*": l.id}) | ||||||
|                 keywords.push(l.title) |                 keywords.push(l.title) | ||||||
|                 keywords.push(l.description) |                 keywords.push(l.description) | ||||||
|  | @ -77,16 +82,19 @@ class LayerOverviewUtils { | ||||||
|         writeFileSync(`./assets/generated/layers/${layer.id}.json`, JSON.stringify(layer, null, "  "), "UTF8"); |         writeFileSync(`./assets/generated/layers/${layer.id}.json`, JSON.stringify(layer, null, "  "), "UTF8"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     getSharedTagRenderings(): Map<string, TagRenderingConfigJson> { |     getSharedTagRenderings(knownImagePaths: Set<string>): Map<string, TagRenderingConfigJson> { | ||||||
|         const dict = new Map<string, TagRenderingConfigJson>(); |         const dict = new Map<string, TagRenderingConfigJson>(); | ||||||
|          |          | ||||||
|  |         const validator = new ValidateTagRenderings(undefined, knownImagePaths); | ||||||
|         for (const key in questions["default"]) { |         for (const key in questions["default"]) { | ||||||
|             if (key === "id") { |             if (key === "id") { | ||||||
|                 continue |                 continue | ||||||
|             } |             } | ||||||
|             questions[key].id = key; |             questions[key].id = key; | ||||||
|             questions[key]["source"] = "shared-questions" |             questions[key]["source"] = "shared-questions" | ||||||
|             dict.set(key, <TagRenderingConfigJson>questions[key]) |             const config = <TagRenderingConfigJson>questions[key] | ||||||
|  |             validator.convertStrict(config, "generate-layer-overview:tagRenderings/questions.json:"+key) | ||||||
|  |             dict.set(key, config) | ||||||
|         } |         } | ||||||
|         for (const key in icons["default"]) { |         for (const key in icons["default"]) { | ||||||
|             if (key === "id") { |             if (key === "id") { | ||||||
|  | @ -96,7 +104,9 @@ class LayerOverviewUtils { | ||||||
|                 continue |                 continue | ||||||
|             } |             } | ||||||
|             icons[key].id = key; |             icons[key].id = key; | ||||||
|             dict.set(key, <TagRenderingConfigJson>icons[key]) |             const config =  <TagRenderingConfigJson>icons[key] | ||||||
|  |             validator.convertStrict(config, "generate-layer-overview:tagRenderings/icons.json:"+key) | ||||||
|  |             dict.set(key,config) | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         dict.forEach((value, key) => { |         dict.forEach((value, key) => { | ||||||
|  | @ -114,9 +124,9 @@ class LayerOverviewUtils { | ||||||
|             .filter(path => path.endsWith(".svg")) |             .filter(path => path.endsWith(".svg")) | ||||||
|             .filter(path => !path.startsWith("./assets/generated")) |             .filter(path => !path.startsWith("./assets/generated")) | ||||||
|         let errCount = 0; |         let errCount = 0; | ||||||
|         const exempt = ["assets/SocialImageTemplate.svg","assets/SocialImageTemplateWide.svg","assets/SocialImageBanner.svg", "assets/svg/osm-logo.svg"]; |         const exempt = ["assets/SocialImageTemplate.svg", "assets/SocialImageTemplateWide.svg", "assets/SocialImageBanner.svg", "assets/svg/osm-logo.svg"]; | ||||||
|         for (const path of allSvgs) { |         for (const path of allSvgs) { | ||||||
|             if(exempt.some(p => "./"+p === path)) { |             if (exempt.some(p => "./" + p === path)) { | ||||||
|                 continue |                 continue | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|  | @ -128,7 +138,7 @@ class LayerOverviewUtils { | ||||||
|                     throw "A core SVG is actually a PNG. Don't do this!" |                     throw "A core SVG is actually a PNG. Don't do this!" | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             if(contents.indexOf("<text")>0){ |             if (contents.indexOf("<text") > 0) { | ||||||
|                 console.warn("The SVG at " + path + " contains a `text`-tag. This is highly discouraged. Every machine viewing your theme has their own font libary, and the font you choose might not be present, resulting in a different font being rendered. Solution: open your .svg in inkscape (or another program), select the text and convert it to a path") |                 console.warn("The SVG at " + path + " contains a `text`-tag. This is highly discouraged. Every machine viewing your theme has their own font libary, and the font you choose might not be present, resulting in a different font being rendered. Solution: open your .svg in inkscape (or another program), select the text and convert it to a path") | ||||||
|                 errCount++; |                 errCount++; | ||||||
| 
 | 
 | ||||||
|  | @ -183,7 +193,7 @@ class LayerOverviewUtils { | ||||||
|         // At the same time, an index of available layers is built.
 |         // At the same time, an index of available layers is built.
 | ||||||
|         console.log("   ---------- VALIDATING BUILTIN LAYERS ---------") |         console.log("   ---------- VALIDATING BUILTIN LAYERS ---------") | ||||||
| 
 | 
 | ||||||
|         const sharedTagRenderings = this.getSharedTagRenderings(); |         const sharedTagRenderings = this.getSharedTagRenderings(knownImagePaths); | ||||||
|         const layerFiles = ScriptUtils.getLayerFiles(); |         const layerFiles = ScriptUtils.getLayerFiles(); | ||||||
|         const sharedLayers = new Map<string, LayerConfigJson>() |         const sharedLayers = new Map<string, LayerConfigJson>() | ||||||
|         const state: DesugaringContext = { |         const state: DesugaringContext = { | ||||||
|  | @ -194,7 +204,7 @@ class LayerOverviewUtils { | ||||||
|         for (const sharedLayerJson of layerFiles) { |         for (const sharedLayerJson of layerFiles) { | ||||||
|             const context = "While building builtin layer " + sharedLayerJson.path |             const context = "While building builtin layer " + sharedLayerJson.path | ||||||
|             const fixed = prepLayer.convertStrict(sharedLayerJson.parsed, context) |             const fixed = prepLayer.convertStrict(sharedLayerJson.parsed, context) | ||||||
|             const validator = new ValidateLayer(sharedLayerJson.path, true); |             const validator = new ValidateLayer(sharedLayerJson.path, true, knownImagePaths); | ||||||
|             validator.convertStrict(fixed, context) |             validator.convertStrict(fixed, context) | ||||||
| 
 | 
 | ||||||
|             if (sharedLayers.has(fixed.id)) { |             if (sharedLayers.has(fixed.id)) { | ||||||
|  | @ -209,24 +219,24 @@ class LayerOverviewUtils { | ||||||
|         return sharedLayers; |         return sharedLayers; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static publicLayerIdsFrom(themefiles: LayoutConfigJson[]): Set<string>{ |     private static publicLayerIdsFrom(themefiles: LayoutConfigJson[]): Set<string> { | ||||||
|         const publicLayers = [].concat(...themefiles |         const publicLayers = [].concat(...themefiles | ||||||
|             .filter(th => !th.hideFromOverview) |             .filter(th => !th.hideFromOverview) | ||||||
|             .map(th => th.layers)) |             .map(th => th.layers)) | ||||||
| 
 | 
 | ||||||
|         const publicLayerIds = new Set<string>() |         const publicLayerIds = new Set<string>() | ||||||
|         for (const publicLayer of publicLayers) { |         for (const publicLayer of publicLayers) { | ||||||
|             if(typeof publicLayer === "string"){ |             if (typeof publicLayer === "string") { | ||||||
|                 publicLayerIds.add(publicLayer) |                 publicLayerIds.add(publicLayer) | ||||||
|                 continue |                 continue | ||||||
|             } |             } | ||||||
|             if(publicLayer["builtin"] !== undefined){ |             if (publicLayer["builtin"] !== undefined) { | ||||||
|                 const bi = publicLayer["builtin"] |                 const bi = publicLayer["builtin"] | ||||||
|                 if(typeof bi === "string"){ |                 if (typeof bi === "string") { | ||||||
|                     publicLayerIds.add(bi) |                     publicLayerIds.add(bi) | ||||||
|                     continue |                     continue | ||||||
|                 } |                 } | ||||||
|                 bi.forEach(id=>publicLayerIds.add(id)) |                 bi.forEach(id => publicLayerIds.add(id)) | ||||||
|                 continue |                 continue | ||||||
|             } |             } | ||||||
|             publicLayerIds.add(publicLayer.id) |             publicLayerIds.add(publicLayer.id) | ||||||
|  | @ -243,7 +253,7 @@ class LayerOverviewUtils { | ||||||
| 
 | 
 | ||||||
|         const convertState: DesugaringContext = { |         const convertState: DesugaringContext = { | ||||||
|             sharedLayers, |             sharedLayers, | ||||||
|             tagRenderings: this.getSharedTagRenderings(), |             tagRenderings: this.getSharedTagRenderings(knownImagePaths), | ||||||
|             publicLayers |             publicLayers | ||||||
|         } |         } | ||||||
|         for (const themeInfo of themeFiles) { |         for (const themeInfo of themeFiles) { | ||||||
|  | @ -251,11 +261,11 @@ class LayerOverviewUtils { | ||||||
|             const themePath = themeInfo.path |             const themePath = themeInfo.path | ||||||
| 
 | 
 | ||||||
|             new PrevalidateTheme().convertStrict(themeFile, themePath) |             new PrevalidateTheme().convertStrict(themeFile, themePath) | ||||||
|             try{ |             try { | ||||||
| 
 | 
 | ||||||
|                 themeFile = new PrepareTheme(convertState).convertStrict(themeFile, themePath) |                 themeFile = new PrepareTheme(convertState).convertStrict(themeFile, themePath) | ||||||
| 
 | 
 | ||||||
|                 if(knownImagePaths === undefined){ |                 if (knownImagePaths === undefined) { | ||||||
|                     throw "Could not load known images/licenses" |                     throw "Could not load known images/licenses" | ||||||
|                 } |                 } | ||||||
|                 new ValidateThemeAndLayers(knownImagePaths, themePath, true, convertState.tagRenderings) |                 new ValidateThemeAndLayers(knownImagePaths, themePath, true, convertState.tagRenderings) | ||||||
|  | @ -263,8 +273,8 @@ class LayerOverviewUtils { | ||||||
| 
 | 
 | ||||||
|                 this.writeTheme(themeFile) |                 this.writeTheme(themeFile) | ||||||
|                 fixed.set(themeFile.id, themeFile) |                 fixed.set(themeFile.id, themeFile) | ||||||
|             }catch(e){ |             } catch (e) { | ||||||
|                 console.error("ERROR: could not prepare theme "+themePath+" due to "+e) |                 console.error("ERROR: could not prepare theme " + themePath + " due to " + e) | ||||||
|                 throw e; |                 throw e; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue