Skip to content

Instantly share code, notes, and snippets.

@Yasir5247
Created September 30, 2022 12:53
Show Gist options
  • Save Yasir5247/401fe0da051dd88726b262a409de9a6e to your computer and use it in GitHub Desktop.
Save Yasir5247/401fe0da051dd88726b262a409de9a6e to your computer and use it in GitHub Desktop.
img.ly integration
const {
withAppBuildGradle,
withProjectBuildGradle,
withMainApplication,
withPlugins,
} = require('@expo/config-plugins');
const withMultidexMainApplication = (config) => {
return withMainApplication(config, (config) => {
config.modResults.contents = addMultidexMainApplication(
config.modResults.contents
);
return config;
});
};
function addMultidexMainApplication(contents) {
// before we replace anything, we check if we already replaced it
if (contents.match('class MainApplication extends androidx.multidex.MultiDexApplication')) {
console.log('\x1b[33m%s\x1b[0m', '✔ imgLy Expo Config Plugin: skipped addMultidexMainApplication. Already found patched MainApplication');
return contents;
}
// the actucal check
const pattern = 'class MainApplication extends Application';
const replacement =
'class MainApplication extends androidx.multidex.MultiDexApplication';
if (contents.match(pattern)) {
return contents.replace(pattern, replacement);
}
throw new Error(
'Cannot setup VideoEditor: MainApplication does not extend Application'
);
}
const withMultidexAppBuildGradle = (config) => {
return withAppBuildGradle(config, (config) => {
config.modResults.contents = addMultidexAppBuildGradle(
config.modResults.contents
);
return config;
});
};
function addMultidexAppBuildGradle(contents) {
// before we replace anything, we check if we already replaced it
if (contents.match('@generated begin imgLyMultidexAppBuild')) {
console.log('\x1b[33m%s\x1b[0m', '✔ imgLy Expo Config Plugin: Skipped addMultidexAppBuildGradle. Already injected.');
return contents;
}
// the actucal check
const addBottom = `
// @generated begin imgLyMultidexAppBuild - expo prebuild - (DO NOT MODIFY)
android {
defaultConfig {
multiDexEnabled true
}
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}
// @generated end imgLyMultidexAppBuild - expo prebuild
`;
return contents.concat(addBottom);
}
const withImgLyProjectBuildGradle = (
config, {
version
}
) => {
return withProjectBuildGradle(config, (config) => {
config.modResults.contents = addImgLy(config.modResults.contents, version);
return config;
});
};
function addImgLy(contents, version) {
// before we replace anything, we check if we already replaced it
if (contents.match('@generated begin addImgLy plugin')) {
console.log('\x1b[33m%s\x1b[0m', '✔ imgLy Expo Config Plugin: Skipped addImgLy. Already found dependencies');
return contents;
}
// the actucal check
let mergedContent = contents;
const addTop = `
// @generated begin addImgLy plugin - expo prebuild - (DO NOT MODIFY)
buildscript {
repositories {
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://artifactory.img.ly/artifactory/imgly" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
classpath 'ly.img.android.sdk:plugin:${version}'
}
}
// @generated end addImgLy - expo prebuild - (DO NOT MODIFY)
`;
const addBottom = `
// @generated begin addImgLy plugin - expo prebuild - (DO NOT MODIFY)
allprojects {
repositories {
maven {
url 'https://artifactory.img.ly/artifactory/imgly'
}
}
}
// @generated end addImgLy - expo prebuild - (DO NOT MODIFY)
`;
mergedContent = addTop.concat(contents);
mergedContent = mergedContent.concat(addBottom);
return mergedContent;
}
const withVESDKConfig = (config) => {
return withAppBuildGradle(config, (config) => {
config.modResults.contents = addVideoEditorSDKConfig(
config.modResults.contents
);
return config;
});
};
function addVideoEditorSDKConfig(contents) {
// before we replace anything, we check if we already replaced it
if (contents.match('@generated begin addVideoEditorSDKConfig')) {
console.log('\x1b[33m%s\x1b[0m', '✔ imgLy Expo Config Plugin: Skipped addVideoEditorSDKConfig. Already found imgly SDK Config');
return contents;
}
// the actucal check
const pattern = 'apply plugin: "com.android.application"';
const addAfter = `
// @generated begin addVideoEditorSDKConfig - expo prebuild - (DO NOT MODIFY)
apply plugin: 'ly.img.android.sdk'
apply plugin: 'kotlin-android'
// Comment out the modules you don't need, to save size.
imglyConfig {
modules {
include 'ui:text'
include 'ui:focus'
include 'ui:frame'
include 'ui:brush'
include 'ui:filter'
include 'ui:sticker'
include 'ui:overlay'
include 'ui:transform'
include 'ui:adjustment'
include 'ui:text-design'
include 'ui:video-trim'
include 'ui:video-library'
include 'ui:video-composition'
include 'ui:audio-composition'
// This module is big, remove the serializer if you don't need that feature.
include 'backend:serializer'
// Remove the asset packs you don't need, these are also big in size.
include 'assets:font-basic'
include 'assets:frame-basic'
include 'assets:filter-basic'
include 'assets:overlay-basic'
include 'assets:sticker-shapes'
include 'assets:sticker-emoticons'
include 'assets:sticker-animated'
include 'backend:sticker-animated'
include 'backend:sticker-smart'
}
}
// @generated end addVideoEditorSDKConfig - expo prebuild - (DO NOT MODIFY)
`;
if (contents.match(pattern)) {
return contents.replace(pattern, pattern + addAfter);
}
throw new Error(
'Cannot setup VideoEditor: plugin "com.android.application" not found'
);
}
const withReactNativeVideoEditorSDK = (
config, {
imgLyVersion = '9.2.0'
} = {}
) => {
return withPlugins(config, [
withMultidexMainApplication,
withMultidexAppBuildGradle,
[withImgLyProjectBuildGradle, {
version: imgLyVersion
}],
withVESDKConfig,
]);
};
module.exports = withReactNativeVideoEditorSDK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment