Giter Site home page Giter Site logo

laravel-file-factory's People

Contributors

adhikasp avatar ajthinking avatar cpsibo avatar mpyw avatar septio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-file-factory's Issues

Merging pipe edits on the same file

Looking at LaravelFileFactory.calculateFiles(), only one pipe can ever touch a given file path and it can only ever generate whole files. If multiple pipes try touching the same path, you end up with duplicate keys in the array that gets shown in the Results tab and one of the generated files is essentially hidden.

This works more or less well enough for items like Models but prevents really working with the app-wide lower-level files like the service providers. If two pipes want to touch the same service provider by inserting different blocks of code in the boot() function, they can't, right now.

So I think the FileFactory/Pipes need to be able to insert select blocks of code at given points in a file. However, a file isn't guaranteed to exist, so they'd also need to be able to fall back to the current functionality of generating the entire file.

Imagine we have an AuthServiceProvider.php.stub like

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Gate::guessPolicyNamesUsing(function ($modelClass) {
            return ___POLICIES_PATH___.class_basename($modelClass).'Policy';
        });

        $this->registerPolicies();
    }
}

This stub could be used by the Policy pipe that I sent in a merge request today since it'd allow the user to specify their desired Policies path under the Settings tab of the webapp. But some other pipe might also want to manually register a policy with this stub:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        Account::class => AccountPolicy::class,
    ];

    public function boot()
    {
        $this->registerPolicies();
    }
}

Our changes don't overlap but the current system prevents us from both registering policies.


There are three ways to solve this that I can think of:

  1. Allow for partial stubs. Have LaravelFileFactory run calculateFiles() for all pipes and flatten the array, then for each pipe run the partial stubs against the calculated files, applying the changes specified.

The drawback of this method is when a file doesn't exist but only has a partial stub. Pipes with partial stubs would basically have to also provide the whole file stub as a fall-back, duplicating code.


  1. Introduce some new markup options in the stubs. For instance, something like
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    ___[___
    public function boot()
    {
    ___]___
        Gate::guessPolicyNamesUsing(function ($modelClass) {
            return 'App\\Policies\\'.class_basename($modelClass).'Policy';
        });

        ___[___
        $this->registerPolicies();
    }
    ___]___
}

Where ___[___ marks the start of necessary scaffolding and ___]___ marks the end. The scaffolding is just the stuff immediately before and after the real changes of the pipe that need to exist for the change to work. But anything between or outside of those scaffolding bits can be changed by other pipes. If the file doesn't exist yet, the stub still has the entire file in it, so the extra markup can just be tossed out.


  1. Use a diffing library to try to merge whole file stubs together. Basically, keep everything like it is now except for adding an extra step in LaravelFileFactory that finds duplicate path entries and tries to merge their content strings together.

This is probably easier than option 2 but would reasonably require adding an external dependency to some 3rd party JS diffing library. I doubt anyone wants to write a diffing facility just for this one spot in the code. It's also probably a bit more fragile than option 2 if two pipes touching the same file are written for different versions of Laravel, thus potentially having different file contents outside of the intended changes (eg. different use statements).


I'd be happy to code this functionality, but I'm not sure which direction you'd want the project to go in, if you even want this functionality at all.

APIResourcePipe.js whenLoaded should be camelCase

Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenLoaded takes the relationship string as a parameter. It should thus format as camelCase, not as snake_case, as can be observed on

return F.singleQuotePad(F.snakeCase(F.pluralize(target.name))) + " => new " + F.pascalCase(target.name) + "Collection($this->whenLoaded(" + F.singleQuotePad(F.snakeCase(F.pluralize(target.name))) + "))"

It can be reproduced by having hasMany relationships to a model that has more than one word, e.g. SomeName, will create a someName() hasMany relation, but in the current pipe, the whenLoaded() will try and load some_name, which is incorrect.

Babel breaked the build?

Recently after a yarn watch restart I could not build laravel-file-factory. Trace:

Anderss-MacBook-Pro:laravel-file-factory anders$ yarn dev
yarn run v1.17.0
$ npm run development
npm WARN lifecycle The node binary used for scripts is /var/folders/9d/8n_tjc1x7pg3vxmqwnm5ml_40000gn/T/yarn--1562597154503-0.5467174983351488/node but npm is using /usr/local/Cellar/node/12.4.0/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> [email protected] development /Users/anders/Code/laravel-file-factory
> cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

/Users/anders/Code/laravel-file-factory/node_modules/webpack-cli/bin/cli.js:93
				throw err;
				^

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at mergeObject (/Users/anders/Code/laravel-file-factory/node_modules/babel-merge/node_modules/deepmerge/dist/umd.js:55:9)
    at deepmerge (/Users/anders/Code/laravel-file-factory/node_modules/babel-merge/node_modules/deepmerge/dist/umd.js:79:10)
    at /Users/anders/Code/laravel-file-factory/node_modules/babel-merge/dist/index.js:30:44
    at Array.reduce (<anonymous>)
    at mergeArray (/Users/anders/Code/laravel-file-factory/node_modules/babel-merge/dist/index.js:20:36)
    at babelMerge (/Users/anders/Code/laravel-file-factory/node_modules/babel-merge/dist/index.js:40:19)
    at /Users/anders/Code/laravel-file-factory/node_modules/babel-merge/dist/index.js:50:15
    at Array.map (<anonymous>)
    at babelMerge (/Users/anders/Code/laravel-file-factory/node_modules/babel-merge/dist/index.js:48:235)
    at /Users/anders/Code/laravel-file-factory/node_modules/babel-merge/dist/index.js:60:26
    at Array.reduce (<anonymous>)
    at Function.value (/Users/anders/Code/laravel-file-factory/node_modules/babel-merge/dist/index.js:58:49)
    at Function.generate (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/BabelConfig.js:11:22)
    at Object.babel (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/config.js:121:45)
    at JavaScript.webpackRules (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/components/JavaScript.js:76:41)
    at ComponentFactory.applyRules (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/components/ComponentFactory.js:155:23)
    at /Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/components/ComponentFactory.js:66:48
    at /Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/Dispatcher.js:34:47
    at Array.forEach (<anonymous>)
    at Dispatcher.fire (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/Dispatcher.js:34:28)
    at Mix.dispatch (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/Mix.js:119:25)
    at WebpackConfig.buildRules (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/builder/WebpackConfig.js:90:13)
    at WebpackConfig.build (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/src/builder/WebpackConfig.js:23:14)
    at Object.<anonymous> (/Users/anders/Code/laravel-file-factory/node_modules/laravel-mix/setup/webpack.config.js:29:38)
    at Module._compile (/Users/anders/Code/laravel-file-factory/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Module.load (internal/modules/cjs/loader.js:641:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:681:19)
    at require (/Users/anders/Code/laravel-file-factory/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
    at WEBPACK_OPTIONS (/Users/anders/Code/laravel-file-factory/node_modules/webpack-cli/bin/utils/convert-argv.js:116:13)
    at requireConfig (/Users/anders/Code/laravel-file-factory/node_modules/webpack-cli/bin/utils/convert-argv.js:118:6)
    at /Users/anders/Code/laravel-file-factory/node_modules/webpack-cli/bin/utils/convert-argv.js:125:17
    at Array.forEach (<anonymous>)
    at module.exports (/Users/anders/Code/laravel-file-factory/node_modules/webpack-cli/bin/utils/convert-argv.js:123:15)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] development: `cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] development script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/anders/.npm/_logs/2019-07-08T14_45_55_974Z-debug.log
error Command failed with exit code 1.

Even when following the installation guide (https://github.com/pipe-dream/docs#pipe-dream-development-guide) the error will appear. Im not quite sure where this was introduced. @adhikasp and @septIO can you reproduce and any clue what might fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.