55
66package com .microsoft .azure .toolkit .intellij .legacy .function .runner .core ;
77
8- import com .google .gson .JsonObject ;
98import com .intellij .codeInsight .AnnotationUtil ;
109import com .intellij .codeInsight .MetaAnnotationUtil ;
1110import com .intellij .lang .jvm .JvmAnnotation ;
1514import com .intellij .openapi .module .ModuleManager ;
1615import com .intellij .openapi .project .Project ;
1716import com .intellij .openapi .roots .CompilerModuleExtension ;
17+ import com .intellij .openapi .roots .LibraryOrderEntry ;
18+ import com .intellij .openapi .roots .ModuleOrderEntry ;
1819import com .intellij .openapi .roots .OrderEnumerator ;
1920import com .intellij .openapi .roots .OrderRootType ;
2021import com .intellij .openapi .roots .libraries .Library ;
2829import com .intellij .psi .search .GlobalSearchScope ;
2930import com .intellij .psi .search .searches .AnnotatedElementsSearch ;
3031import com .intellij .util .containers .ContainerUtil ;
32+ import com .intellij .workspaceModel .ide .impl .legacyBridge .module .roots .ModuleOrderEntryBridge ;
33+ import com .microsoft .azure .toolkit .intellij .common .AzureArtifactManager ;
3134import com .microsoft .azure .toolkit .intellij .common .AzureBundle ;
3235import com .microsoft .azure .toolkit .lib .Azure ;
3336import com .microsoft .azure .toolkit .lib .AzureConfiguration ;
5255import org .jetbrains .idea .maven .project .MavenProject ;
5356import org .jetbrains .idea .maven .project .MavenProjectsManager ;
5457
58+ import javax .annotation .Nonnull ;
5559import javax .annotation .Nullable ;
5660import java .io .File ;
5761import java .io .IOException ;
6872import java .util .LinkedHashMap ;
6973import java .util .List ;
7074import java .util .Map ;
75+ import java .util .Objects ;
76+ import java .util .Optional ;
7177import java .util .concurrent .Callable ;
78+ import java .util .regex .Matcher ;
79+ import java .util .regex .Pattern ;
7280import java .util .stream .Collectors ;
7381
7482import static com .microsoft .azure .toolkit .intellij .common .AzureBundle .message ;
7785public class FunctionUtils {
7886 private static final int MAX_PORT = 65535 ;
7987
80- public static final String FUNCTION_JAVA_LIBRARY_ARTIFACT_ID = "azure-functions-java-library" ;
8188 private static final String AZURE_FUNCTION_ANNOTATION_CLASS =
8289 "com.microsoft.azure.functions.annotation.FunctionName" ;
8390 private static final String FUNCTION_JSON = "function.json" ;
@@ -94,6 +101,7 @@ public class FunctionUtils {
94101 private static final String AZURE_FUNCTIONS_APP_SETTINGS = "Azure Functions App Settings" ;
95102 private static final String AZURE_FUNCTIONS_JAVA_LIBRARY = "azure-functions-java-library" ;
96103 private static final String AZURE_FUNCTIONS_JAVA_CORE_LIBRARY = "azure-functions-java-core-library" ;
104+ private static final Pattern ARTIFACT_NAME_PATTERN = Pattern .compile ("(.*)-(\\ d+\\ .)?(\\ d+\\ .)?(\\ *|\\ d+).*" );
97105
98106 static {
99107 //initialize required attributes, which will be saved to function.json even if it equals to its default value
@@ -179,7 +187,7 @@ public static boolean isFunctionProject(Project project) {
179187 }
180188 final List <Library > libraries = new ArrayList <>();
181189 OrderEnumerator .orderEntries (project ).productionOnly ().forEachLibrary (library -> {
182- if (StringUtils .contains (library .getName (), FUNCTION_JAVA_LIBRARY_ARTIFACT_ID )) {
190+ if (StringUtils .containsAnyIgnoreCase (library .getName (), AZURE_FUNCTIONS_JAVA_LIBRARY , AZURE_FUNCTIONS_JAVA_CORE_LIBRARY )) {
183191 libraries .add (library );
184192 }
185193 return true ;
@@ -287,26 +295,53 @@ public static Map<String, FunctionConfiguration> prepareStagingFolder(Path stagi
287295 if (gradleProject .isValid ()) {
288296 gradleProject .getDependencies ().forEach (lib -> dependencies .add (lib ));
289297 } else {
290- OrderEnumerator .orderEntries (module ).productionOnly ().forEachLibrary (lib -> {
291- Arrays .stream (lib .getFiles (OrderRootType .CLASSES )).map (virtualFile -> new File (stripExtraCharacters (virtualFile .getPath ())))
292- .filter (File ::exists )
293- .forEach (dependencies ::add );
298+ OrderEnumerator .orderEntries (module ).productionOnly ().forEach (lib -> {
299+ if (lib instanceof ModuleOrderEntry ) {
300+ Optional .ofNullable (getArtifactFromModule (((ModuleOrderEntry ) lib ).getModule ())).ifPresent (dependencies ::add );
301+ } else if (lib instanceof LibraryOrderEntry ) {
302+ Arrays .stream (lib .getFiles (OrderRootType .CLASSES )).map (virtualFile -> new File (stripExtraCharacters (virtualFile .getPath ())))
303+ .filter (File ::exists )
304+ .forEach (dependencies ::add );
305+ }
294306 return true ;
295307 });
296308 }
297309 final String libraryToExclude = dependencies .stream ()
298- .filter (artifact -> StringUtils .equalsAnyIgnoreCase (artifact .getName (), AZURE_FUNCTIONS_JAVA_CORE_LIBRARY ))
299- .map (File ::getName ).findFirst ().orElse (AZURE_FUNCTIONS_JAVA_LIBRARY );
310+ .map (FunctionUtils ::getArtifactIdFromFile )
311+ .filter (name -> StringUtils .equalsAnyIgnoreCase (name , AZURE_FUNCTIONS_JAVA_CORE_LIBRARY ))
312+ .findFirst ().orElse (AZURE_FUNCTIONS_JAVA_LIBRARY );
300313
301314 final File libFolder = new File (stagingFolder .toFile (), "lib" );
302315 for (final File file : dependencies ) {
303- if (!StringUtils .containsIgnoreCase (file .getName (), libraryToExclude )) {
316+ if (!StringUtils .equalsIgnoreCase (getArtifactIdFromFile (file ), libraryToExclude )) {
317+ if (!file .exists ()) {
318+ throw new AzureToolkitRuntimeException (String .format ("Dependency artifact (%s) not found, please correct the dependency and try again" , file .getAbsolutePath ()));
319+ }
304320 FileUtils .copyFileToDirectory (file , libFolder );
305321 }
306322 }
307323 return configMap ;
308324 }
309325
326+ // get artifact based on module
327+ @ Nullable
328+ private static File getArtifactFromModule (final Module module ) {
329+ final Project project = module .getProject ();
330+ final AzureArtifactManager artifactManager = AzureArtifactManager .getInstance (project );
331+ return artifactManager .getAllSupportedAzureArtifacts ().stream ()
332+ .filter (artifact -> Objects .equals (artifactManager .getModuleFromAzureArtifact (artifact ), module ))
333+ .findFirst ()
334+ .map (azureArtifact -> artifactManager .getFileForDeployment (azureArtifact ))
335+ .map (File ::new )
336+ .orElse (null );
337+ }
338+
339+ private static String getArtifactIdFromFile (@ Nonnull final File file ) {
340+ final Matcher matcher = ARTIFACT_NAME_PATTERN .matcher (file .getName ());
341+ return matcher .matches () ? StringUtils .substringBeforeLast (file .getName (), "-" ) :
342+ StringUtils .substringBeforeLast (file .getName (), ".jar" );
343+ }
344+
310345 public static String getTargetFolder (Module module ) {
311346 if (module == null ) {
312347 return StringUtils .EMPTY ;
0 commit comments