// ĠCopyright Ġ 2 0 0 0 - 2 0 1 9 ĠJetBrains Ġs . r . o . ĠUse Ġof Ġthis Ġsource Ġcode Ġis Ġgoverned Ġby Ġthe ĠApache Ġ 2 . 0 Ġlicense Ġthat Ġcan Ġbe Ġfound Ġin Ġthe ĠLICENSE Ġfile . Ċ @ file : Jvm Name (" Gradle Project Import Util ") Ċ package Ġorg . jetbrains . plugins . gradle . service . project . open Ċ Ċ import Ġcom . intellij . ide . General Settings Ċ import Ġcom . intellij . ide . highlighter . Project FileType Ċ import Ġcom . intellij . ide . impl . Project Util Ċ import Ġcom . intellij . openapi . components . Service Manager Ċ import Ġcom . intellij . openapi . external System . model . External System Data Keys Ċ import Ġcom . intellij . openapi . external System . service . execution . External System J dk Util Ċ import Ġcom . intellij . openapi . external System . service . execution . Progress Execution Mode . IN _ BACKGROUND _ ASYNC Ċ import Ġcom . intellij . openapi . external System . service . execution . Progress Execution Mode . MOD AL _ SYNC Ċ import Ġcom . intellij . openapi . external System . service . project . manage . External Projects ManagerImpl Ċ import Ġcom . intellij . openapi . external System . settings . External Project Settings Ċ import Ġcom . intellij . openapi . external System . util . External System Api Util Ċ import Ġcom . intellij . openapi . external System . util . External System Util Ċ import Ġcom . intellij . openapi . project . D umb Service Ċ import Ġcom . intellij . openapi . project . Project Ċ import Ġcom . intellij . openapi . project . Project Manager Ċ import Ġcom . intellij . openapi . project . ex . Project Manager Ex Ċ import Ġcom . intellij . openapi . project Roots . Project J dk Table Ċ import Ġcom . intellij . openapi . project Roots . Sdk Ċ import Ġcom . intellij . openapi . project Roots . Simple Java Sdk Type Ċ import Ġcom . intellij . openapi . roots . Project Root Manager Ċ import Ġcom . intellij . openapi . util . io . File Util Ċ import Ġcom . intellij . openapi . vfs . Virtual File Ċ import Ġcom . intellij . platform . Platform Project Open Processor Ċ import Ġcom . intellij . util . Environment Util Ċ import Ġcom . intellij . util . Three State . YES Ċ import Ġcom . intellij . util . io . exists Ċ import Ġcom . intellij . util . text . nul lize Ċ import Ġorg . gradle . util . Gradle Version Ċ import Ġorg . jetbrains . plugins . gradle . service . Gradle Installation Manager Ċ import Ġorg . jetbrains . plugins . gradle . settings . Distribution Type Ċ import Ġorg . jetbrains . plugins . gradle . settings . Distribution Type . DEFAULT _ WRAP PED Ċ import Ġorg . jetbrains . plugins . gradle . settings . Gradle Project Settings Ċ import Ġorg . jetbrains . plugins . gradle . settings . Gradle Settings Ċ import Ġorg . jetbrains . plugins . gradle . startup . Gradle Un linked Project Processor Ċ import Ġorg . jetbrains . plugins . gradle . util . Gradle Constants Ċ import Ġorg . jetbrains . plugins . gradle . util . Gradle Constants . SYSTEM _ ID Ċ import Ġorg . jetbrains . plugins . gradle . util . Gradle Environment . Head less .* Ċ import Ġorg . jetbrains . plugins . gradle . util . Gradle Log . LOG Ċ import Ġorg . jetbrains . plugins . gradle . util . Gradle Util Ċ import Ġjava . nio . file . Invalid Path Exception Ċ import Ġjava . nio . file . Paths Ċ Ċ private Ġfun Ġis Gradle Project File ( file : ĠVirtual File ): ĠBoolean Ġ{ ĊĠ Ġreturn Ġ! file . isDirectory Ġ&& ĠGradle Constants . BUILD _ FILE _ EXTENSION S . any Ġ{ Ġfile . name . endsWith ( it ) Ġ} Ċ } Ċ Ċ fun Ġcan Import Project From ( file : ĠVirtual File ): ĠBoolean Ġ{ ĊĠ Ġif Ġ(! file . isDirectory ) Ġreturn Ġis Gradle Project File ( file ) ĊĠ Ġreturn Ġfile . children . any (:: is Gradle Project File ) Ċ } Ċ Ċ fun Ġopen Project ( project File : ĠVirtual File , Ġproject To Close : ĠProject ?, Ġforce Open In New Frame : ĠBoolean ): ĠProject ? Ġ{ ĊĠ Ġif Ġ(! can Import Project From ( project File )) Ġreturn Ġnull ĊĠ Ġval Ġproject Directory Ġ= Ġfind External Project Directory ( project File ) ĊĠ Ġif Ġ( focus On Opened Same Project ( project Directory . path )) Ġreturn Ġnull ĊĠ Ġif Ġ( can Open Platform Project ( project Directory )) Ġ{ ĊĠĠĠ Ġreturn Ġopen Platform Project ( project Directory , Ġproject To Close , Ġforce Open In New Frame ) ĊĠ Ġ} ĊĠ Ġval Ġproject Ġ= Ġcreate Project ( project Directory ) Ġ?: Ġreturn Ġnull ĊĠ Ġimport Project ( project Directory . path , Ġproject ) ĊĠ Ġif Ġ(! force Open In New Frame ) Ġclose Previous Project ( project To Close ) ĊĠ ĠProject Manager Ex . getInstance Ex (). open Project ( project ) ĊĠ Ġreturn Ġproject Ċ } Ċ Ċ fun Ġimport Project ( project Directory : ĠString , Ġproject : ĠProject ) Ġ{ ĊĠ ĠLOG . info (" Import Ġproject Ġat Ġ$ project Directory ") ĊĠ Ġval Ġproject Sdk Ġ= ĠProject Root Manager . getInstance ( project ). project Sdk ĊĠ Ġval Ġgradle Project Settings Ġ= ĠGradle Project Settings () ĊĠ Ġsetup Gradle Settings ( gradle Project Settings , Ġproject Directory , Ġproject , Ġproject Sdk ) ĊĠ Ġattach Gradle Project And Refresh ( gradle Project Settings , Ġproject ) ĊĠ ĠProject Util . update Last Project Location ( project Directory ) ĊĠ Ġproject . save () Ċ } Ċ Ċ private Ġfun Ġcan Open Platform Project ( project Directory : ĠVirtual File ): ĠBoolean Ġ{ ĊĠ Ġif Ġ(! Platform Project Open Processor . getInstance (). can Open Project ( project Directory )) Ġreturn Ġfalse ĊĠ Ġif Ġ( is Child Exists Using Io ( project Directory , ĠProject . DIRECTORY _ STORE _ FOLDER )) Ġreturn Ġtrue ĊĠ Ġif Ġ( is Child Exists Using Io ( project Directory , Ġproject Directory . name Ġ+ ĠProject FileType . DOT _ DEFAULT _ EXTENSION )) Ġreturn Ġtrue ĊĠ Ġreturn Ġfalse Ċ } Ċ Ċ private Ġfun Ġis Child Exists Using Io ( parent : ĠVirtual File , Ġname : ĠString ): ĠBoolean Ġ{ ĊĠ Ġreturn Ġtry Ġ{ ĊĠĠĠ ĠPaths . get ( File Util . to System Dependent Name ( parent . path ), Ġname ). exists () ĊĠ Ġ} ĊĠ Ġcatch Ġ( e : ĠInvalid Path Exception ) Ġ{ ĊĠĠĠ Ġfalse ĊĠ Ġ} Ċ } Ċ Ċ private Ġfun Ġfocus On Opened Same Project ( project Directory : ĠString ): ĠBoolean Ġ{ ĊĠ Ġfor Ġ( project Ġin ĠProject Manager . getInstance (). open Projects ) Ġ{ ĊĠĠĠ Ġif Ġ( Project Util . is Same Project ( project Directory , Ġproject )) Ġ{ ĊĠĠĠĠĠ ĠProject Util . focus Project Window ( project , Ġfalse ) ĊĠĠĠĠĠ Ġreturn Ġtrue ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĠ Ġreturn Ġfalse Ċ } Ċ Ċ private Ġfun Ġopen Platform Project ( project Directory : ĠVirtual File , Ġproject To Close : ĠProject ?, Ġforce Open In New Frame : ĠBoolean ): ĠProject ? Ġ{ ĊĠ Ġval Ġopen Processor Ġ= ĠPlatform Project Open Processor . getInstance () ĊĠ Ġreturn Ġopen Processor . do Open Project ( project Directory , Ġproject To Close , Ġforce Open In New Frame )?. also Ġ{ ĊĠĠĠ ĠGradle Un linked Project Processor . enable Notifications ( it ) ĊĠ Ġ} Ċ } Ċ Ċ fun Ġattach Gradle Project And Refresh ( settings : ĠExternal Project Settings , Ġproject : ĠProject ) Ġ{ ĊĠ Ġval Ġexternal Project Path Ġ= Ġsettings . external Project Path ĊĠ ĠExternal Projects ManagerImpl . getInstance ( project ). run When Initialized Ġ{ ĊĠĠĠ ĠD umb Service . getInstance ( project ). run When Smart Ġ{ ĊĠĠĠĠĠ ĠExternal System Util . ensure Tool Window Initialized ( project , ĠSYSTEM _ ID ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĠ ĠExternal System Api Util . get Settings ( project , ĠSYSTEM _ ID ). link Project ( settings ) ĊĠ ĠExternal System Util . refresh Project ( project , ĠSYSTEM _ ID , Ġexternal Project Path , Ġtrue , ĠMOD AL _ SYNC ) ĊĠ ĠExternal System Util . refresh Project ( project , ĠSYSTEM _ ID , Ġexternal Project Path , Ġfalse , ĠIN _ BACKGROUND _ ASYNC ) Ċ } Ċ Ċ private Ġfun Ġclose Previous Project ( project To Close : ĠProject ?) Ġ{ ĊĠ Ġval Ġopen Projects Ġ= ĠProject Manager . getInstance (). open Projects ĊĠ Ġif Ġ( open Projects . isNotEmpty ()) Ġ{ ĊĠĠĠ Ġval Ġexit Code Ġ= ĠProject Util . confirm Open New Project ( true ) ĊĠĠĠ Ġif Ġ( exit Code Ġ== ĠGeneral Settings . OPEN _ PROJECT _ SAME _ WINDOW ) Ġ{ ĊĠĠĠĠĠ ĠProject Util . close And Dispose ( project To Close Ġ?: Ġopen Projects [ open Projects . size Ġ- Ġ 1 ]) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ Ċ private Ġfun Ġfind External Project Directory ( file : ĠVirtual File ): ĠVirtual File Ġ{ ĊĠ Ġif Ġ(! file . isDirectory ) Ġreturn Ġfile . parent ĊĠ Ġreturn Ġfile Ċ } Ċ Ċ private Ġfun Ġcreate Project ( project Directory : ĠVirtual File ): ĠProject ? Ġ{ ĊĠ Ġval Ġproject Manager Ġ= ĠProject Manager Ex . getInstance Ex () ĊĠ Ġval Ġproject Ġ= Ġproject Manager . create Project ( project Directory . name , Ġproject Directory . path ) ĊĠ Ġproject ?. put UserData ( External System Data Keys . NEW LY _ IMPORTED _ PROJECT , Ġtrue ) ĊĠ Ġreturn Ġproject Ċ } Ċ Ċ fun Ġsetup Gradle Settings ( settings : ĠGradle Project Settings , Ġproject Directory : ĠString , Ġproject : ĠProject , Ġproject Sdk : ĠS dk ? Ġ= Ġnull ) Ġ{ ĊĠ ĠGradle Settings . getInstance ( project ). setup Gradle Settings () ĊĠ Ġsettings . setup Gradle Project Settings ( project Directory , Ġproject , Ġproject Sdk ) Ċ } Ċ Ċ private Ġfun ĠGradle Settings . setup Gradle Settings () Ġ{ ĊĠ Ġgradle Vm Options Ġ= ĠGR AD LE _ VM _ OPTIONS Ġ?: Ġgradle Vm Options ĊĠ Ġis Offline Work Ġ= ĠGR AD LE _ OFF LINE ?. to Boolean () Ġ?: Ġis Offline Work ĊĠ Ġservice DirectoryPath Ġ= ĠGR AD LE _ SERVICE _ DIRECTORY Ġ?: Ġservice DirectoryPath ĊĠ Ġif Ġ( External System Util . isNew Project ( project ) Ġ&& Ġlinked Projects Settings . isEmpty ()) Ġ{ ĊĠĠĠ ĠExternal Projects ManagerImpl . getInstance ( project ). set Store Ex tern ally ( true ) ĊĠ Ġ} Ċ } Ċ Ċ private Ġfun ĠGradle Project Settings . setup Gradle Project Settings ( project Directory : ĠString , Ġproject : ĠProject , Ġproject Sdk : ĠS dk ? Ġ= Ġnull ) Ġ{ ĊĠ Ġexternal Project Path Ġ= Ġproject Directory ĊĠ Ġis Use Auto Import Ġ= Ġfalse ĊĠ Ġis Create Empty Content Root Directories Ġ= Ġfalse ĊĠ Ġis Use Qualified Module Names Ġ= Ġtrue ĊĠ Ġstore Project Files Ex tern ally Ġ= ĠYES ĊĠ Ġdistribution Type Ġ= ĠGR AD LE _ DISTRIB UTION _ TYPE ?. let ( Distribution Type :: valueOf ) Ġ?: ĠDEFAULT _ WRAP PED ĊĠ Ġgradle Home Ġ= ĠGR AD LE _ HOME Ġ?: Ġsuggest Gradle Home () ĊĠ Ġwith ( Settings Context ( project , Ġproject Sdk , Ġresolve Gradle Version ())) Ġ{ ĊĠĠĠ Ġgradle Jvm Ġ= Ġget Project J dk () Ġ?: Ġget Most Recent J dk () Ġ?: Ġget Java Home J dk () ĊĠ Ġ} Ċ } Ċ Ċ private Ġfun Ġsuggest Gradle Home (): ĠString ? Ġ{ ĊĠ Ġval Ġinstallation Manager Ġ= ĠService Manager . getService ( Gradle Installation Manager :: class . java ) ĊĠ Ġval Ġlast Used Gradle Home Ġ= ĠGradle Util . getLast Used Gradle Home (). nul lize () ĊĠ Ġif Ġ( last Used Gradle Home Ġ!= Ġnull ) Ġreturn Ġlast Used Gradle Home ĊĠ Ġval Ġgradle Home Ġ= Ġinstallation Manager . aut ode tected Gradle Home Ġ?: Ġreturn Ġnull ĊĠ Ġreturn ĠFile Util . to Canonical Path ( gradle Home . path ) Ċ } Ċ Ċ private Ġclass ĠSettings Context ( val Ġproject : ĠProject , Ġval Ġproject Sdk : ĠS dk ?, Ġval Ġgradle Version : ĠGradle Version ) Ċ Ċ private Ġfun ĠSettings Context . get Java Home J dk (): ĠString ? Ġ{ ĊĠ Ġval Ġjava Home Ġ= ĠEnvironment Util . get Environment Map ()[" JAVA _ HOME "] Ġ?: Ġreturn Ġnull ĊĠ Ġval Ġj dk Ġ= ĠGradle J dk . valueOf ( java Home ) Ġ?: Ġreturn Ġnull ĊĠ Ġif Ġ(! jdk . is Supported ( gradle Version )) Ġreturn Ġnull ĊĠ Ġval Ġsimple Java Sdk Type Ġ= ĠSimple Java Sdk Type . getInstance () ĊĠ Ġval Ġsdk Name Ġ= Ġsimple Java Sdk Type . suggest Sdk Name ( null , Ġjava Home ) ĊĠ Ġsimple Java Sdk Type . create J dk ( sdk Name , Ġjava Home ) ĊĠ Ġreturn ĠExternal System J dk Util . USE _ JAVA _ HOME Ċ } Ċ Ċ private Ġfun ĠSettings Context . getProject J dk (): ĠString ? Ġ{ ĊĠ Ġval Ġproject Sdk Ġ= Ġproject Sdk Ġ?: ĠProject Root Manager . getInstance ( project ). project Sdk ĊĠ Ġval Ġproject J dk Ġ= Ġproject Sdk ?. let ( Gradle J dk . Companion :: valueOf ) Ġ?: Ġreturn Ġnull ĊĠ Ġif Ġ(! project J dk . is Supported ( gradle Version )) Ġreturn Ġnull ĊĠ Ġreturn ĠExternal System J dk Util . USE _ PROJECT _ JDK Ċ } Ċ Ċ private Ġfun ĠSettings Context . get Most Recent J dk (): ĠString ? Ġ{ ĊĠ Ġval Ġproject J dk Table Ġ= ĠProject J dk Table . getInstance () ĊĠ Ġval Ġjava Sdk Type Ġ= ĠExternal System J dk Util . get Java Sdk Type () ĊĠ Ġval Ġj dk Ġ= Ġproject J dk Table . get Sdk s OfType ( java Sdk Type ) ĊĠĠĠ Ġ. map NotNull Ġ{ ĠGradle J dk . valueOf ( it ) Ġ} ĊĠĠĠ Ġ. filter Ġ{ Ġit . is Supported ( gradle Version ) Ġ} ĊĠĠĠ Ġ. max By Ġ{ Ġit . version Ġ} ĊĠ Ġreturn Ġj dk ?. name Ċ } Ċ