-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a simple NpmManager (#2028)
* Add npm packager manager classes * Rename .java to .kt * Remove GsonDependency, remove rednundant classes * Import downloaded ig * Import downloaded ig * Add SimplePackageCacheManager * Remove npmpackage from npmpackagemanager * Add npm manager * Implement NpmManager * Rollback version updates * Address review suggestion: - Rename ImplementationGuide.kt to Dependency.kt - Drop the NpmPackageManager.kt * Spottless apply * Fix tests * Use current module in workflow library * Run spotless apply * Remove print statement * Use kotlin stream functions * Remove dead code * Remove dead code * Move FhirOperatorBuilder into FhirOperator.kt file * Address comments and renaming --------- Co-authored-by: Jing Tang <[email protected]>
- Loading branch information
1 parent
6b43d65
commit ddb2a7f
Showing
22 changed files
with
654 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
knowledge/src/main/java/com/google/android/fhir/knowledge/FhirNpmPackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.fhir.knowledge | ||
|
||
/** | ||
* A FHIR NPM Package as defined by the FHIR specification. | ||
* | ||
* See https://hl7.org/fhir/packages.html for the published FHIR NPM Packages specification. | ||
* | ||
* See https://confluence.hl7.org/display/FHIR/NPM+Package+Specification for more info under the | ||
* management of FHIR Infrastructure. | ||
*/ | ||
data class FhirNpmPackage(val name: String, val version: String, val canonical: String? = null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
knowledge/src/main/java/com/google/android/fhir/knowledge/npm/NpmFileManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.fhir.knowledge.npm | ||
|
||
import com.google.android.fhir.knowledge.FhirNpmPackage | ||
import java.io.File | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.json.JSONObject | ||
|
||
/** Manages stored NPM packages. */ | ||
internal class NpmFileManager(private val cacheRoot: File) { | ||
|
||
/** | ||
* Returns the NpmPackage for the given [packageId] and [version] from cache or `null` if the | ||
* package is not cached. | ||
*/ | ||
suspend fun getPackage(packageId: String, version: String): LocalFhirNpmPackageMetadata { | ||
return withContext(Dispatchers.IO) { | ||
val packageFolder = File(getPackageFolder(packageId, version), "package") | ||
readNpmPackage(packageFolder) | ||
} | ||
} | ||
|
||
/** | ||
* Returns the NpmPackage for the given [packageId] and [version] from cache or `null` if the | ||
* package is not cached. | ||
*/ | ||
suspend fun containsPackage(packageId: String, version: String): Boolean { | ||
return withContext(Dispatchers.IO) { | ||
val packageFolder = File(getPackageFolder(packageId, version), "package") | ||
val packageJson = File(packageFolder, "package.json") | ||
packageJson.exists() | ||
} | ||
} | ||
|
||
/** Returns the package folder for the given [packageId] and [version]. */ | ||
fun getPackageFolder(packageId: String, version: String) = File(cacheRoot, "$packageId#$version") | ||
|
||
/** Creates an [LocalFhirNpmPackageMetadata] parsing the package manifest file. */ | ||
private fun readNpmPackage(packageFolder: File): LocalFhirNpmPackageMetadata { | ||
val packageJson = File(packageFolder, "package.json") | ||
val json = JSONObject(packageJson.readText()) | ||
with(json) { | ||
val dependenciesList = optJSONObject("dependencies") | ||
val dependencies = | ||
dependenciesList?.keys()?.asSequence()?.map { key -> | ||
FhirNpmPackage(key, dependenciesList.getString(key)) | ||
} | ||
|
||
return LocalFhirNpmPackageMetadata( | ||
getString("name"), | ||
getString("version"), | ||
optString("canonical"), | ||
dependencies?.toList() ?: emptyList(), | ||
packageFolder, | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.