import { readdirSync } from "fs" ; import { readJsonSync , readJson , existsSync } from "fs-extra" ; import { resolve , dirname } from "path" ; import { filter , reduce , has , forEach , remove , get , find } from "lodash" ; import { FetchResponse } from "@ui5-language-assistant/language-server" ; import { UI5SemanticModel } from "@ui5-language-assistant/semantic-model-types" ; import { generateFunc , TestModelVersion , TypeNameFix , Json } from "../../api" ; import { addUi5Resources } from "./download-ui5-resources" ; const MODEL_CACHE = Object . create ( null ) ; const fixes = { "1.60.14" : { "{sap.ui.layout.cssgrid.IGridConfigurable}" : undefined , "sap.m.IHyphenation" : undefined , "sap.ui.core.IDScope" : undefined , "sap.m.TimePickerSlider" : undefined , "sap.ui.layout.ResponsiveSplitterPage" : undefined , "sap.ui.layout.cssgrid.CSSGridTrack" : undefined , "sap.ui.layout.cssgrid.CSSGridGapShortHand" : undefined , "sap.ui.layout.cssgrid.CSSGridLine" : undefined , "sap.gantt.def.SvgDefs" : undefined , "sap.gantt.simple.UtilizationChart" : undefined , "sap.ui.suite.ui.commons.statusindicator.SimpleShape" : undefined , "sap.ui.fl.transport.FlTransportDialog" : undefined , "sap.ui.mdc.EditMode" : undefined , "sap.ui.mdc.base.FieldHelpBase" : undefined , "sap.ui.vk.TransformationMatrix" : undefined , "sap.ui.base.BaseObject" : undefined , "sap.ui.core.EventProvider" : undefined , "sap.ui.vk." : "sap.ui.vk.Material" , Service : undefined , Control : "sap.ui.core.Control" , Element : "sap.ui.core.Element" , array : "any[]" , Array : "any[]" , bloolean : "boolean" , "sap.gantt.misc.AxisTime" : "sap.gantt.misc.AxisTimes" , "sap.gantt.control.Toolbar" : undefined , "sap.gantt.DragOrientation" : undefined , "sap.gantt.simple.GanttHeader" : undefined , "sap.gantt.simple.InnerGanttChart" : undefined , "sap.rules.ui.RuleBase" : undefined , "sap.ui.generic.app.transaction.BaseController" : undefined , "sap.ui.vk.tools.HitTestIdMode" : undefined , "sap.ui.vk.tools.CoordinateSystem" : undefined , "sap.ui.vk.SelectionMode" : undefined , "sap.viz.ui5.controls.VizRangeSlider" : undefined , } , "1.74.0" : { "sap.m.PlanningCalendarHeader" : undefined , "sap.m.TimePickerSlider" : undefined , "sap.ui.fl.write._internal.transport.TransportDialog" : undefined , "sap.ui.layout.cssgrid.IGridItemLayoutData" : undefined , "sap.ui.layout.ResponsiveSplitterPage" : undefined , "sap.ui.integration.designtime.BaseEditor" : undefined , "sap.ui.vk.AnimationPlayer" : undefined , "sap.ui.vk.IPlaybackCollection" : undefined , "sap.ui.vk.ViewManager" : undefined , Control : "sap.ui.core.Control" , Element : "sap.ui.core.Element" , array : "any[]" , Array : "any[]" , bloolean : "boolean" , "sap.gantt.misc.AxisTime" : "sap.gantt.misc.AxisTimes" , "sap.gantt.control.Toolbar" : undefined , "sap.gantt.DragOrientation" : undefined , "sap.gantt.simple.GanttHeader" : undefined , "sap.gantt.simple.InnerGanttChart" : undefined , "sap.rules.ui.RuleBase" : undefined , "sap.ui.generic.app.transaction.BaseController" : undefined , "sap.ui.vk.tools.HitTestIdMode" : undefined , "sap.ui.vk.tools.CoordinateSystem" : undefined , "sap.ui.vk.SelectionMode" : undefined , "sap.viz.ui5.controls.VizRangeSlider" : undefined , } , "1.75.0" : { "sap.m.PlanningCalendarHeader" : undefined , "sap.m.TimePickerSlider" : "sap.m.TimePickerSliders" , "sap.ui.fl.write._internal.transport.TransportDialog" : undefined , "sap.ui.layout.cssgrid.IGridItemLayoutData" : undefined , "sap.ui.layout.ResponsiveSplitterPage" : undefined , "Object.<string,any>" : undefined , "sap.gantt.control.Toolbar" : undefined , } , "1.71.14" : { Control : "sap.ui.core.Control" , Element : "sap.ui.core.Element" , array : "any[]" , Array : "any[]" , bloolean : "boolean" , "sap.m.PlanningCalendarHeader" : undefined , "sap.m.TimePickerSlider" : undefined , "sap.ui.layout.ResponsiveSplitterPage" : undefined , "sap.gantt.misc.AxisTime" : "sap.gantt.misc.AxisTimes" , "sap.gantt.control.Toolbar" : undefined , "sap.gantt.DragOrientation" : undefined , "sap.gantt.simple.GanttHeader" : undefined , "sap.gantt.simple.InnerGanttChart" : undefined , "sap.rules.ui.RuleBase" : undefined , "sap.ui.generic.app.transaction.BaseController" : undefined , "sap.ui.vk.BillboardTextEncoding" : undefined , "sap.ui.vk.BillboardStyle" : undefined , "sap.ui.vk.BillboardBorderLineStyle" : undefined , "sap.ui.vk.BillboardHorizontalAlignment" : undefined , "sap.ui.vk.BillboardCoordinateSpace" : undefined , "sap.ui.vk.DetailViewType" : undefined , "sap.ui.vk.DetailViewShape" : undefined , "sap.ui.vk.tools.HitTestIdMode" : undefined , "sap.ui.vk.tools.CoordinateSystem" : undefined , "sap.ui.vk.AnimationTimeSlider" : undefined , "sap.ui.vk.SelectionMode" : undefined , "sap.ui.vk.RenderMode" : undefined , "sap.viz.ui5.controls.VizRangeSlider" : undefined , } , } ; function getModelFolder ( version ) { const pkgJsonPath = require . resolve ( "@ui5-language-assistant/test-utils/package.json" ) ; const rootPkgFolder = dirname ( pkgJsonPath ) ; const modelFolder = resolve ( rootPkgFolder , ".model-cache" , version ) ; return modelFolder ; } export function getTypeNameFixForVersion ( version ) { return fixes [ version ] ; } export async function readTestLibraryFile ( version , fileName ) { try { const inputFolder = getModelFolder ( version as TestModelVersion ) ; const filePath = resolve ( inputFolder , fileName ) ; const ok = existsSync ( filePath ) ; return { ok : ok , json : ( ) : Promise < unknown > => readJson ( filePath ) , } ; } catch ( error ) { return { ok : false , json : ( ) : never => { throw error ; } , } ; } } const downloadedLibrariesPromises = { } ; export async function downloadLibraries ( version ) { if ( ! has ( downloadedLibrariesPromises , version ) ) { downloadedLibrariesPromises [ version ] = addUi5Resources ( version , getModelFolder ( version ) ) ; } return downloadedLibrariesPromises [ version ] ; } function loadLibraries ( version ) { const inputFolder = getModelFolder ( version ) ; const files = readdirSync ( inputFolder ) ; const LIBFILE_SUFFIX = ".designtime.api.json" ; const libFiles = filter ( files , ( _ ) => _ . endsWith ( LIBFILE_SUFFIX ) ) ; const libToFileContent = reduce ( libFiles , ( libToFileContentMap , file ) => { const libName = file . substring ( 0 , file . length - LIBFILE_SUFFIX . length ) ; libToFileContentMap [ libName ] = readJsonSync ( resolve ( inputFolder , file ) ) ; return libToFileContentMap ; } , Object . create ( null ) ) ; return libToFileContent ; } export async function generateModel ( { version , downloadLibs = true , strict = true , modelGenerator , } ) { const useCache = strict === true ; if ( has ( MODEL_CACHE , version ) && useCache ) { return MODEL_CACHE [ version ] ; } if ( downloadLibs ) { await downloadLibraries ( version ) ; } const libToFileContent = loadLibraries ( version ) ; if ( strict ) { fixLibraries ( version , libToFileContent ) ; } const model = modelGenerator ( { version : version , libraries : libToFileContent , typeNameFix : getTypeNameFixForVersion ( version ) , strict : strict , printValidationErrors : strict , } ) ; if ( useCache ) { MODEL_CACHE [ version ] = model ; } return model ; } type LibraryFix = ( content : Json ) => void ; const libraryFixes = { "1.60.14" : { "sap.ushell" : [ ( content ) : void => { const symbol = find ( get ( content , "symbols" ) , ( symbol ) => symbol . name === "sap.ushell.services.EndUserFeedback" ) ; const method = find ( get ( symbol , "methods" ) , ( method ) => method . name === "getLegalText" ) ; remove ( method . parameters , ( parameter ) => get ( parameter , "name" ) === "" ) ; } , ] , } , "1.71.14" : { } , "1.74.0" : { "sap.ui.generic.app" : [ ( content ) : void => { remove ( get ( content , "symbols" ) , ( symbol ) => get ( symbol , "name" ) === "sap.ui.generic.app.navigation.service.NavigationHandler" ) ; } , ] , "sap.ushell" : [ ( content ) : void => { const symbols = get ( content , "symbols" ) ; remove ( symbols , ( symbol ) => get ( symbol , "basename" ) === "" ) ; } , ] , } , "1.75.0" : { } , } ; function fixLibraries ( version , libToFileContent ) { const fixesForLib = libraryFixes [ version ] ; forEach ( fixesForLib , ( fixes , library ) => { if ( has ( libToFileContent , library ) ) { forEach ( fixes , ( fix ) => fix ( libToFileContent [ library ] ) ) ; } } ) ; }