Skip to content

Commit

Permalink
chore: re-organize project structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
lilingxi01 committed Jan 16, 2023
1 parent ec05a82 commit 319a67e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 127 deletions.

This file was deleted.

91 changes: 0 additions & 91 deletions .swiftpm/xcode/xcshareddata/xcschemes/swift.xcscheme

This file was deleted.

22 changes: 0 additions & 22 deletions Package.swift

This file was deleted.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Radix Colors is a great color palette for software design and development. But i

This library is intended to provide a simple way to use Radix Colors in SwiftUI. It parses the original library and generates a Swift file that contains all the colors. Alongside all colors, it also provides a set of extension variables for SwiftUI built-in Color module, which supports dynamic color (a mix of both light color and dark color for light mode and dark mode respectively).

## Naming Convention
## Usage

This is a cross-language project, which contains two different naming conventions. So, when a folder is intended to be used in TypeScript, it will be named in camelCase. Otherwise, for Swift, it will be named in PascalCase.
The file `RadixColors.swift` (located in `output` folder) is generated by parsing the original library. It contains all the colors in Radix Colors.

To use Radix Colors in your SwiftUI project, simply copy the `RadixColors.swift` file into your project. The reason why we do not want to make it a Swift Package is that we want to avoid any redundant `import` statement in your code. We want you to be able to use it directly and globally, and can change some helper functions as you wish.

## Reference

Expand Down
12 changes: 12 additions & 0 deletions generator/header-comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require('../package.json');

export const headerComment = `//
// RadixColors.swift
// version: ${packageJson.version}
//
// To update this file, visit: https://github.com/lilingxi01/radix-colors-swift
//
// Created by Lingxi Li on 1/15/23.
//
`;
12 changes: 8 additions & 4 deletions generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as radixColors from '@radix-ui/colors';
import fs from 'fs';
import { helperExtensions } from './helper';
import { headerComment } from './header-comment';

/**
* This function takes a color string (the value of each Radix Color) and convert it to Swift Color.
* @param colorString - The value of a Radix Color
* @return - A script of Swift Color in Swift.
*/
function colorStringToHSL(colorString: string): string {
function parseColorString(colorString: string): string {
// Remove prefix and suffix.
let parsedString = colorString
.replace('hsl(', '')
Expand Down Expand Up @@ -36,8 +37,8 @@ function colorStringToHSL(colorString: string): string {
return `Color(hue: ${h}, saturation: ${s.toFixed(3)}, brightness: ${l.toFixed(3)})`;
}

const outputDir = 'Sources/RadixColors';
const outputFile = 'Core.swift';
const outputDir = 'output';
const outputFile = 'RadixColors.swift';

function main() {
// Create the output directory if it does not exist.
Expand All @@ -54,6 +55,9 @@ function main() {
// Get all entries from Radix Colors library.
const radixEntries = Object.entries(radixColors);

// Write the header comment.
logger.write(headerComment + '\n');

// Write the header of the file.
logger.write('import SwiftUI\n\n');

Expand All @@ -62,7 +66,7 @@ function main() {
for (const [name, colors] of radixEntries) {
logger.write(' class ' + name + ' {\n');
for (const [colorName, color] of Object.entries(colors)) {
logger.write(' static let ' + colorName + ': Color = ' + colorStringToHSL(color) + '\n');
logger.write(' static let ' + colorName + ': Color = ' + parseColorString(color) + '\n');
}
logger.write(' }\n');
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/RadixColors/Core.swift → output/RadixColors.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
//
// RadixColors.swift
// version: 1.0.0
//
// To update this file, visit: https://github.com/lilingxi01/radix-colors-swift
//
// Created by Lingxi Li on 1/15/23.
//

import SwiftUI

final class RadixColor {
Expand Down

0 comments on commit 319a67e

Please sign in to comment.