spider 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 75

Banner

Spider #

A small dart library to generate Assets dart code from assets folder. It generates dart class with static const variables in it which can be used to reference the assets safely anywhere in the flutter app.

Example #

Before

Widget build(BuildContext context) {
  return Image(image: AssetImage('assets/background.png'));
}

After

Widget build(BuildContext context) {
  return Image(image: AssetImage(Assets.background));
}

Generated Assets Class

class Assets {
  static const String background = 'assets/background.png';
}

This method allows no error scope for string typos. Also, it provides auto-complete in the IDE which comes very handy when you have large amount of assets.

Installation #

This is package is an independent library that is not linked to your project. So there's no need to add it to your flutter project as it works as a global command line tool for all of your projects.

pub global activate spider

Run following command to see help:

spider --help

Usage #

Create Configuration File

Spider provides a very easy and straight forward way to create a configuration file. Execute following command and it will create a configuration file with default configurations in it.

spider create

Now you can modify available configurations and Spider will use those configs when generating dart code.

Use JSON config file

Though above command creates YAML format for config file, spider also supports JSON format for config file. Use this command to create JSON config file instead of YAML.

spider create --json

No matter which config format you use, JSON or YAML, spider automatically detects it and uses it for code generation.

Here's the default configuration that will be in the config file:

groups:
  - path: assets/images
    class_name: Images
    package: res

Generate Code #

Run following command to generate dart code:

spider build

Manual #

| KEY | TYPE | DEFAULT VALUE | SCOPE | DESCRIPTION | |----------------- |-------------- |---------------- |-------- |------------------------------------------------------------------------------------------------------- | | path/paths* | String | None | GROUP | Where to locate assets? | | class_name* | String | None | GROUP | What will be the name of generated dart class? | | package | String | resources | GLOBAL | Where to generate dart code in the lib folder? | | file_name | String | {class_name} | GROUP | What will be the name of the generated dart file? | | prefix | String | None | GROUP | What will be the prefix of generated dart references? | | types | List

Watch Directory #

Spider can also watch given directory for changes in files and rebuild dart code automatically. Use following command to watch for changes:

spider build --watch

see help for more information:

spider build --help

Smart Watch (Experimental) #

The normal --watch option watches for any kind of changes that happens in the directory. However this can be improved my smartly watching the directory. It includes ignoring events that doesn't affect anything like
file content changes. Also, it only watches allowed file types and
rebuilds upon changes for those files only.

Run following command to watch directories smartly.

spider build --smart-watch

Categorizing by File Extension #

By default, Spider allows any file to be referenced in the dart code. but you can change that behavior. You can specify which files you want to be referenced.

path: assets
class_name: Assets
package: res
types: [ jpg, png, jpeg, webp, bmp, gif ]

Use Prefix #

You can use prefixes for names of the generated dart references. Prefixes will be attached to the formatted reference names.

path: assets
class_name: Assets
package: res
prefix: ic
Output
class Assets {
  static const String icCamera = 'assets/camera.png';
  static const String icLocation = 'assets/location.png';
}

Advanced Configuration #

Spider provides supports for multiple configurations and classifications. If you wanna group your assets by module, type or anything, you can do that using groups in spider.

Example #

Suppose you have both vector(SVGs) and raster images in your project and you want to me classified separately so that you can use them with separate classes. You can use groups here. Keep your vector and raster images in separate folder and specify them in the config file.

spider.yaml

groups:
  - path: assets/images
    class_name: Images
    package: res
  - path: assets/vectors
    class_name: Svgs
    package: res

Here, first item in the list indicates to group assets of assets/images folder under class named Images and the second one indicates to group assets of assets/vectors directory under class named Svgs.

So when you refer to Images class, auto-complete suggests raster images only and you know that you can use them with AssetImage and other one with vector rendering library.

Multi-path configuration #

From Spider v0.4.0, multiple paths can be specified for a single group
to collect references from multiple directories and generate all the
references under single dart class.

Example

groups:
  - paths:
      - assets/images
      - assets/more_images/
    class_name: Images
    package: res
    types: [ .png, .jpg, .jpeg, .webp, .webm, .bmp ]

By using paths, multiple source directories can be specified. Above example will generate references from assets/images and
assets/more_images/ under a single dart class named Images.

Generating Tests #

Spider v0.4.0 adds support for generating test cases for generated
dart references to make sure that the asset file is present in the
project. These tests can also be run on CI servers. To enable tests generation, specify generate_tests flag in spider.yaml or spider.json configuration file as shown below.

generate_tests: true

This flag will indicate spider to generate tests for all the generated
dart references.

Enable Verbose Logging #

Spider prefers not to overwhelm terminal with verbose logs that are redundant for most of the cases. However those verbose logs come quite handy when it comes to debug anything. You can enable verbose logging by using --verbose option on build command.

spider build --verbose

# watching directories with verbose logs
spider build --watch --verbose

License #

Copyright © 2020 Birju Vachhani

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License 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.

1.0.0 #

  • Added support for exporting generated dart code which is enabled by default This can be helpful in cases where you want to use a single file to import all of the generated classes. (Accessible individual classes when importing)

  • Added support to use opt in for usage of part of feature of dart. It allows to avoid false imports when using export option. It makes all the generated dart code files to behave like one file and one import.

  • Added support to remove Generated by spider... comment line from all the generated dart code. Allows to minimize vcs noise.

  • export_file can be used to provide name of the export file.

Breaking Changes

  • Instead of providing package to every group, now you have to define global package name as it makes more sense. Providing package name for individual groups won't work.

0.5.0 #

  • Added support for check updates
  • Updated help manuals
  • Updated example configs files
  • Fixed verbose logs

0.4.1 #

  • Fix build command failing when there's no test generation specified
  • Fix embedded version
  • Added test to make sure that release version and embedded version matches

0.4.0 #

  • Spider now allows to specify multiple paths to generate dart references under a single class.
  • Spider now generates test cases for dart references to make sure that the file is present in the project.

0.3.6 #

  • Fixed issue of creating references for files like .DS_Store
  • Now Spider shows error if you try to create a group with flutter specific assets directories like 2.0x and 3.0x.

0.3.5 #

  • fixes common commands execution issue. Now you can execute command anywhere you like.

0.3.4 #

  • Fix build command when there are sub-directories in assets directories

0.3.3 #

  • Added smart watch feature.
  • use --smart-watch option when running build command to enable it.

0.3.2 #

  • Fix create command

0.3.1 #

  • Fix version command.
  • Formatted outputs and added more verbose logs.
  • Added config validation before processing assets.

0.3.0 #

  • Add support for categorization by file types.
  • Add support for using prefixes for generated dart references.

0.2.1 #

  • Add support for JSON format for config files
  • Add flag --json to create command to create json config file.
  • Update readme to add json option for config file.

0.2.0 #

  • Add support for multiple assets directories
  • Add support for separate class generation
  • Uses sample config file config creation

0.1.3 #

  • Add support for watching directories for file changes
  • Rename init command to create command
  • Implemented verbose flag for build command
  • add build command

0.1.2 #

  • add emojis for console logs

0.1.1 #

  • fix issues of pub.dev health report
  • refactor code

0.1.0 #

  • added dart class generator
  • fix pub.dev warnings
  • add code documentation

0.0.1 #

  • pre-alpha release
  • Initial version for demo purpose
  • avoid using in production.

example/EXAMPLE.md

Basic Example #

Just use following command:

spider build

Before

Widget build(BuildContext context) {
  return Image(image: AssetImage('assets/background.png'));
}

After

Widget build(BuildContext context) {
  return Image(image: AssetImage(Assets.background));
}

Generated Assets Class

class Assets {
  static const String background = 'assets/background.png';
}

Use this package as an executable

1. Install it

You can install the package from the command line:


$ pub global activate spider

2. Use it

The package has the following executables:


$ spider

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  spider: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:spider/spider.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Jun 5, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Health suggestions

Format lib/src/data/yaml_config.dart.

Run dartfmt to format lib/src/data/yaml_config.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
args ^1.6.0 1.6.0
dart_style ^1.3.6 1.3.6
html ^0.14.0+3 0.14.0+3
http ^0.12.1 0.12.1
logging ^0.11.4 0.11.4
path ^1.7.0 1.7.0
watcher ^0.9.7+15 0.9.7+15
yaml ^2.2.1 2.2.1
Transitive dependencies
_fe_analyzer_shared 4.0.0
analyzer 0.39.10
async 2.4.1
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
glob 1.2.0
http_parser 3.1.4
js 0.6.1+1
meta 1.1.8
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
pub_semver 1.4.4
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
build_runner ^1.9.0
build_version ^2.0.1
pedantic ^1.9.0 1.9.0
test ^1.14.3