import { BuildType , Mode , NATIVE_ALL } from '../types' ; import type { ScriptState } from '../BuildScript' ; import type { Binding , BindingDefinition , BindingMapUnsafe , ModeDefinition , Native , PlatformSelection , Version , } from '../types' ; import { generateGradle } from './gradle' ; import { generateIvy } from './ivy' ; import { generateMaven } from './maven' ; export function copyToClipboard ( ref ) { const selection = window . getSelection ( ) ; if ( selection === null ) { return ; } if ( selection . rangeCount > 0 ) { selection . removeAllRanges ( ) ; } const range = document . createRange ( ) ; const pre = ref . current ; if ( pre === null ) { return ; } range . selectNode ( pre ) ; selection . addRange ( range ) ; document . execCommand ( 'copy' ) ; selection . removeAllRanges ( ) ; alert ( 'Script copied to clipboard.' ) ; } export function mime ( mode ) { return mode . file !== undefined && mode . file . endsWith ( '.xml' ) ? 'text/xml' : 'text/plain' ; } export function isNativeApplicableToAllPlatforms ( artifact , platform ) { if ( artifact . natives === undefined || artifact . nativesOptional !== true ) { return true ; } let applicablePlatforms = artifact . natives . filter ( ( p ) => platform [ p ] ) . length ; return applicablePlatforms === 0 || applicablePlatforms === NATIVE_ALL . filter ( ( p ) => platform [ p ] ) . length ; } export function getArtifactName ( binding ) { return binding . id === 'lwjgl' ? binding . id : binding . id . substring ( 'lwjgl-' . length ) ; } export function getVersion ( version , build ) { return build === BuildType . Release ? version : `${ version }-SNAPSHOT` ; } export function getSelectedPlatforms ( natives , platform ) { let result = null ; for ( let i = 0 ; i < natives . length ; i += 1 ) { const p = natives [ i ] ; if ( platform [ p ] ) { if ( result === null ) { result = p ; } else { return null ; } } } return result ; } export function generateScript ( mode , state ) { switch ( mode ) { case Mode . Maven : return generateMaven ( state ) ; case Mode . Gradle : return generateGradle ( state ) ; case Mode . Ivy : return generateIvy ( state ) ; default : throw 'Unsupported script mode' ; } } export function generateDependencies ( selected , artifacts , platform , osgi , generateJava , generateNative ? ) { let script = '' ; let nativesBundle = '' ; const groupId = osgi ? 'org.lwjgl.osgi' : 'org.lwjgl' ; selected . forEach ( ( binding ) => { let artifact = artifacts [ binding ] ; let hasEnabledNativePlatform = false ; if ( artifact . natives !== undefined ) { hasEnabledNativePlatform = artifact . natives . some ( ( it ) => platform [ it ] ) ; if ( ! hasEnabledNativePlatform && artifact . nativesOptional !== true ) { return ; } } const artifactId = osgi ? `org.lwjgl.${ getArtifactName ( artifact ) }` : binding ; script += generateJava ( artifact , groupId , artifactId , hasEnabledNativePlatform ) ; if ( ! osgi && hasEnabledNativePlatform && generateNative !== undefined ) { nativesBundle += generateNative ( artifact , groupId , artifactId ) ; } } ) ; return script + nativesBundle ; }