Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions plugins/withMediaButtonModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,18 @@ const withMediaButtonModule = (config) => {

// Check if MediaButtonPackage is already imported/added
if (!mainApplication.contents.includes('MediaButtonPackage')) {
// Get the package name from the first line of the file
const packageMatch = mainApplication.contents.match(/^package\s+([^\s]+)/);
const packageName = packageMatch ? packageMatch[1] : 'com.resgrid.unit';

// Add import statement for MediaButtonPackage after the package declaration
const importStatement = `import ${packageName}.MediaButtonPackage`;
if (!mainApplication.contents.includes(importStatement)) {
// Add import after the package declaration line
mainApplication.contents = mainApplication.contents.replace(/^(package\s+[^\n]+\n)/, `$1${importStatement}\n`);
console.log('[withMediaButtonModule] Added MediaButtonPackage import');
Comment on lines +484 to +493
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Handle non-first-line package declarations and avoid false “import added” logs.

If MainApplication.kt starts with a BOM, blank line, or header comment, ^package won’t match, so the import won’t be inserted even though the log says it was. Consider a multiline-safe match and guard if the package declaration isn’t found.

🛠️ Suggested fix
-      const packageMatch = mainApplication.contents.match(/^package\s+([^\s]+)/);
+      const packageMatch = mainApplication.contents.match(/^\s*package\s+([A-Za-z0-9_.]+)\s*$/m);
       const packageName = packageMatch ? packageMatch[1] : 'com.resgrid.unit';

       // Add import statement for MediaButtonPackage after the package declaration
       const importStatement = `import ${packageName}.MediaButtonPackage`;
-      if (!mainApplication.contents.includes(importStatement)) {
-        // Add import after the package declaration line
-        mainApplication.contents = mainApplication.contents.replace(/^(package\s+[^\n]+\n)/, `$1${importStatement}\n`);
-        console.log('[withMediaButtonModule] Added MediaButtonPackage import');
-      }
+      if (!mainApplication.contents.includes(importStatement)) {
+        if (packageMatch) {
+          // Add import after the package declaration line
+          mainApplication.contents = mainApplication.contents.replace(
+            /^\s*package\s+[^\n]+\n/m,
+            (match) => `${match}${importStatement}\n`
+          );
+          console.log('[withMediaButtonModule] Added MediaButtonPackage import');
+        } else {
+          console.warn('[withMediaButtonModule] Package declaration not found; skipped MediaButtonPackage import');
+        }
+      }
🤖 Prompt for AI Agents
In `@plugins/withMediaButtonModule.js` around lines 484 - 493, The current package
detection uses a line-start regex that fails if the file has a BOM, blank lines,
or comments, causing the import not to be inserted while still logging success;
update the logic that computes packageMatch/mainApplication.contents: use a
multiline-tolerant regex (e.g., match first occurrence of
/^\s*package\s+([^\s]+)/m) or locate the package declaration with a search for
"package " and capture its end position, then only perform the replace/insert of
importStatement and call console.log('[withMediaButtonModule] Added
MediaButtonPackage import') if the contents were actually modified (i.e.,
packageMatch found and mainApplication.contents changed); keep references to
packageMatch, packageName, importStatement and the replace insertion logic in
withMediaButtonModule.

}

// Add the package to getPackages()
// Find the packages list and add our package
const packagesPattern = /val packages = PackageList\(this\)\.packages(\.toMutableList\(\))?/;
Expand Down
Loading