Skip to content

Commit

Permalink
Merge pull request #1385 from xaviPastHubs/patch-1
Browse files Browse the repository at this point in the history
feat: add accessControl field to Android Compose template
  • Loading branch information
jorenbroekema authored Nov 19, 2024
2 parents 03e0403 + 8c966ed commit feaf2e4
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-mails-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

add accessControl field to Android Compose template
10 changes: 6 additions & 4 deletions __integration__/__snapshots__/compose.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ snapshots["integration compose compose/object should match snapshot"] =
package com.example.tokens;
package com.example.tokens
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
Expand Down Expand Up @@ -176,7 +176,8 @@ object StyleDictionary {
val sizePaddingMedium = 16.00.dp
val sizePaddingSmall = 8.00.dp
val sizePaddingXl = 16.00.dp
}`;
}
`;
/* end snapshot integration compose compose/object should match snapshot */

snapshots["integration compose compose/object with references should match snapshot"] =
Expand All @@ -186,7 +187,7 @@ snapshots["integration compose compose/object with references should match snaps
package com.example.tokens;
package com.example.tokens
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
Expand Down Expand Up @@ -354,6 +355,7 @@ object StyleDictionary {
val colorFontInteractiveHover = colorBrandPrimary
val colorFontInteractiveActive = colorBrandSecondary
val colorFontInteractiveDisabled = colorFontTertiary
}`;
}
`;
/* end snapshot integration compose compose/object with references should match snapshot */

14 changes: 8 additions & 6 deletions __tests__/formats/__snapshots__/all.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,16 @@ snapshots["formats all should match compose/object snapshot"] =
package ;
package
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
object {
object {
/** comment */
val color_red = #FF0000
}`;
}
`;
/* end snapshot formats all should match compose/object snapshot */

snapshots["formats all should match ios/macros snapshot"] =
Expand Down Expand Up @@ -1082,15 +1083,16 @@ snapshots["formats all should match compose/object snapshot with fileHeaderTimes
package ;
package
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
object {
object {
/** comment */
val color_red = #FF0000
}`;
}
`;
/* end snapshot formats all should match compose/object snapshot with fileHeaderTimestamp set */

snapshots["formats all should match ios/macros snapshot with fileHeaderTimestamp set"] =
Expand Down
48 changes: 48 additions & 0 deletions __tests__/formats/__snapshots__/androidCompose.test.snap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* @web/test-runner snapshot v1 */
export const snapshots = {};

snapshots["formats compose/object should match default snapshot"] =
`
// Do not edit directly, this file was auto-generated.
package
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
object {
/** comment */
val color-base-red = #ff0000
val color-white = #ffffff
val size-font-large = 18rem
val size-font-small = 12rem
}
`;
/* end snapshot formats compose/object should match default snapshot */

snapshots["formats compose/object with options overrides should match snapshot"] =
`
// Do not edit directly, this file was auto-generated.
package com.example.tokens
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.unit.*
public data object MyObject {
/** comment */
public val color-base-red = #ff0000
public val color-white = #ffffff
public val size-font-large = 18rem
public val size-font-small = 12rem
}
`;
/* end snapshot formats compose/object with options overrides should match snapshot */

114 changes: 114 additions & 0 deletions __tests__/formats/androidCompose.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.
*/
import { expect } from 'chai';
import formats from '../../lib/common/formats.js';
import createFormatArgs from '../../lib/utils/createFormatArgs.js';
import flattenTokens from '../../lib/utils/flattenTokens.js';

const tokens = {
size: {
font: {
small: {
value: '12rem',
type: 'fontSize',
original: {
value: '12px',
},
name: 'size-font-small',
path: ['size', 'font', 'small'],
},
large: {
value: '18rem',
type: 'fontSize',
original: {
value: '18px',
},
name: 'size-font-large',
path: ['size', 'font', 'large'],
},
},
},
color: {
base: {
red: {
value: '#ff0000',
type: 'color',
comment: 'comment',
original: {
value: '#FF0000',
comment: 'comment',
},
name: 'color-base-red',
path: ['color', 'base', 'red'],
},
},
white: {
value: '#ffffff',
type: 'color',
original: {
value: '#ffffff',
},
name: 'color-white',
path: ['color', 'white'],
},
},
};

const format = formats['compose/object'];
const file = {
destination: '__output/',
format: 'compose/object',
};

describe('formats', () => {
describe(`compose/object`, () => {
it('should match default snapshot', async () => {
const f = await format(
createFormatArgs({
dictionary: { tokens, allTokens: flattenTokens(tokens) },
file,
platform: {},
}),
{},
file,
);
await expect(f).to.matchSnapshot();
});

it('with options overrides should match snapshot', async () => {
const file = {
options: {
import: [
'androidx.compose.ui.graphics.Color',
'androidx.compose.ui.graphics.Brush',
'androidx.compose.ui.unit.*',
],
className: 'MyObject',
packageName: 'com.example.tokens',
accessControl: 'public',
objectType: 'data',
},
};
const f = await format(
createFormatArgs({
dictionary: { tokens, allTokens: flattenTokens(tokens) },
file,
platform: {},
}),
{},
file,
);
await expect(f).to.matchSnapshot();
});
});
});
2 changes: 2 additions & 0 deletions docs/src/content/docs/reference/Hooks/Formats/predefined.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ Creates a Kotlin file for Compose containing an object with a `val` for each pro
| `options.outputReferences` | `boolean \| OutputReferencesFunction` | Whether or not to keep [references](/reference/hooks/formats#references-in-output-files) (a -> b -> c) in the output. Defaults to `false`. Also allows passing a function to conditionally output references on a per token basis. |
| `options.className` | `string` | The name of the generated Kotlin object |
| `options.packageName` | `string` | The package for the generated Kotlin object |
| `options.accessControl` | `string` | Level of access of the generated compose properties and object. Defaults to `''`. |
| `options.objectType` | `string` | Type of Kotlin object. Defaults to `''` (regular object). |

Example:

Expand Down
12 changes: 8 additions & 4 deletions lib/common/templates/compose/object.kt.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
export default ({ allTokens, formatProperty, options, header }) => `
${header}
package ${options.packageName ?? ''};
package ${options.packageName ?? ''}
${options.import.map(/** @param {string} item */ (item) => `import ${item}`).join('\n')}
object ${options.className ?? ''} {
${options.accessControl ? `${options.accessControl} ` : ''}${
options.objectType ? `${options.objectType} ` : ''
}object ${options.className ? `${options.className} ` : ''}{
${allTokens
.map(
(token) =>
`${token.comment ? ` /** ${token.comment} */\n` : ''} val ${formatProperty(token)}`,
` ${token.comment ? `/** ${token.comment} */\n ` : ''}${options.accessControl ? `${options.accessControl} ` : ''}val ${formatProperty(
token,
)}`,
)
.join('\n')}
}`;
}\n`;

0 comments on commit feaf2e4

Please sign in to comment.