Skip to content

Commit

Permalink
feat(formats): compose object imports (#874)
Browse files Browse the repository at this point in the history
Co-authored-by: Giovanni Martusciello <[email protected]>
  • Loading branch information
giovannimartusciello and Giovanni Martusciello authored Jan 29, 2023
1 parent e1626a7 commit 8373721
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/common/formatHelpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
sortByReference: require('./sortByReference'),
sortByName: require('./sortByName'),
minifyDictionary: require('./minifyDictionary'),
setSwiftFileProperties: require('./setSwiftFileProperties')
setSwiftFileProperties: require('./setSwiftFileProperties'),
setComposeObjectProperties: require('./setComposeObjectProperties')
}
30 changes: 30 additions & 0 deletions lib/common/formatHelpers/setComposeObjectProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

/**
* Outputs an object for compose format configurations. Sets import.
* @memberof module:formatHelpers
* @param {Object} options - The options object declared at configuration
* @returns {Object}
*/
function setComposeObjectProperties(options) {
if (typeof options.import === 'undefined') {
options.import = ['androidx.compose.ui.graphics.Color', 'androidx.compose.ui.unit.*'];
} else if (typeof options.import === 'string') {
options.import = [options.import];
}

return options
}

module.exports = setComposeObjectProperties;
5 changes: 4 additions & 1 deletion lib/common/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const fs = require('fs');
const path = require('path');
const _template = require('lodash/template');
const GroupMessages = require('../utils/groupMessages');
const { fileHeader, formattedVariables, getTypeScriptType, iconsWithPrefix, minifyDictionary, sortByReference, createPropertyFormatter, sortByName, setSwiftFileProperties } = require('./formatHelpers');
const { fileHeader, formattedVariables, getTypeScriptType, iconsWithPrefix, minifyDictionary, sortByReference, createPropertyFormatter, sortByName, setSwiftFileProperties, setComposeObjectProperties } = require('./formatHelpers');

const SASS_MAP_FORMAT_DEPRECATION_WARNINGS = GroupMessages.GROUP.SassMapFormatDeprecationWarnings;

Expand Down Expand Up @@ -708,6 +708,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul
* @param {String} className The name of the generated Kotlin object
* @param {String} packageName The package for the generated Kotlin object
* @param {Object} options
* * @param {String[]} [options.import=androidx.compose.ui.unit.*] - Modules to import. Can be a string or array of string
* @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date
* @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
* @example
Expand Down Expand Up @@ -742,6 +743,8 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul
allProperties = [...dictionary.allProperties].sort(sortByName);
}

options = setComposeObjectProperties(options);

return template({allProperties, file, options, formatProperty, fileHeader});
},

Expand Down
7 changes: 4 additions & 3 deletions lib/common/templates/compose/object.kt.template
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

package <%= file.packageName %>;

<% // Ideally we would selectively import the correct classes & functions based on whether the applied transforms actually need them. %>
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
<%= options.import.map(function(item) {
return 'import ' + item
}).join('\n')
%>

object <%= file.className %> {
<%= allProperties.map(function(prop) {
Expand Down

0 comments on commit 8373721

Please sign in to comment.